我正在尝试合并两个xml文件,如下所示,但我无法获得所需的输出请帮助我谢谢
Java代码:
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setIgnoringComments(true);
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse(new File("file1.xml"));
Document doc1 = builder.parse(new File("file2.xml"));
NodeList nodes = doc.getElementsByTagName("staff");
NodeList nodes1 = doc1.getElementsByTagName("staff");
for(int i=0;i<nodes1.getLength();i=i+1){
Node n= (Node) doc.importNode(nodes1.item(i), true);
nodes.item(i).getParentNode().appendChild(n);
}
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
Writer output = null;
output = new BufferedWriter(new FileWriter("mergedxml.xml"));
String xmlOutput = result.getWriter().toString();
output.write(xmlOutput);
output.close();
System.out.println("merge complete");
Run Code Online (Sandbox Code Playgroud)
File1.xml
<company>
<staff>
<name>john</name>
<phone>465456433</phone>
<email>gmail1</email> …Run Code Online (Sandbox Code Playgroud) 我想使用Jackson创建简单的JSON对象,我不需要为每个响应构建自定义类,而是一个类似于下面代码的预制对象.其他JSON库(android,JSON.org,GSON)你可以做类似的事情
JsonObject myObject = new JsonObject("{\"a\":1}");
myObject.getInt("a"); // returns 1
Run Code Online (Sandbox Code Playgroud)
我似乎无法在Jackson套餐中找到类似的操作.PS:我知道我可以创建一个java类来封装这个特定的JSON字符串,但我正在寻找的是一种创建通用JSON对象的方法,我不需要将其解析为我定义的类.我似乎无法在互联网上找到任何指向类似于此的东西.我有一种感觉,这是在Jacksons领域之外,他们不支持这样的操作.如果是这种情况就这么说,我将结束这个问题.
我的目标是在我的项目中没有另一个Json库.
编辑2014:我发现您可以使用org.codehaus.jackson.node.ObjectNode将保留您的对象的类,并允许您按我的问题中所述进行操作.
下面是代码示例:
ObjectMapper mapper = new ObjectMapper();
ObjectNode myObject = (ObjectNode) mapper.readTree("{\"a\":1}");
System.out.println(myObject.get("a").asInt()); // prints 1
Run Code Online (Sandbox Code Playgroud) 我使用Google Closure Compiler使用PHP自动编译javascript(需要这样做 - 在PHP中,在Windows机器上没有安全限制).我编写了简单的PHP脚本来调用进程,将.js内容传递给stdin并通过stdout接收重新编译的.js.它工作正常,问题是,当我编译例如40 .js文件时,它需要强大的机器将近2分钟.但是,市长延迟是因为java为每个脚本启动.jar应用程序的新实例.有没有办法如何修改下面的脚本来创建进程只有一个并在进程结束前多次发送/接收.js内容?
function compileJScript($s) {
$process = proc_open('java.exe -jar compiler.jar', array(
0 => array("pipe", "r"), 1 => array("pipe", "w")), $pipes);
if (is_resource($process)) {
fwrite($pipes[0], $s);
fclose($pipes[0]);
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
if (proc_close($process) == 0) // If fails, keep $s intact
$s = $output;
}
return $s;
}
Run Code Online (Sandbox Code Playgroud)
我可以看到几个选项,但不知道是否可能以及如何做到这一点:
我正在使用 hibernate 来存储和检索 MySQL 数据库中的数据。我使用的是字节数组,但遇到了 SerialBlob 类。我可以成功使用该类,但我似乎找不到使用 SerialBlob 和字节数组之间的任何区别。有谁知道您希望使用 byte[] 的 SerialBlob inlue 的基本区别或可能的情况是什么?