我正在尝试将多个xml合并到一个框架xml文件中,以创建一个新的XML文件。我正在尝试创建一个包含多个xml并合并为一个xml的循环。我正在尝试这样的事情,但它不起作用 :File dir = new File(“ E:\ temp \”); File [] rootFiles = dir.listFiles(); 到目前为止,我无法使用单个XML创建正确的xml。任何帮助将不胜感激。
这是我的带有单个xml输入的代码:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File("Result.xml");
String baseXml = "E:\\IntelliJ\\TryXml\\src\\blank.xml";
String inputXmls = "E:\\IntelliJ\\TryXml\\src\\input1.xml";
Document doc1 = db.parse(baseXml);
Document doc2 = db.parse(inputXmls);
NodeList nList = doc1.getElementsByTagName("Configs");
Element element = (Element) nList.item(0);
Node copiedNode = doc2.importNode(element, true);
doc2.getDocumentElement().appendChild(copiedNode);
processXml(doc2, file);
}
private static void processXml(Document xml, File file) throws TransformerException {
Transformer tf = TransformerFactory.newInstance().newTransformer();
tf.setOutputProperty(OutputKeys.INDENT,"yes");
tf.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,"yes");
DOMSource source = new DOMSource(xml);
StreamResult result = new StreamResult(file);
tf.transform(source,result);
}
XML文件
Input1.xml
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<Configs>
<Config name="test1">
<title>Title 1</title>
<author>Author1</author>
</Config>
<Config name="test2">
<title>Title 2</title>
<author>Author2</author>
</Config>
</Configs>
<optional>I dont want this to be copied</optional>
<Ratings>
<body>
<Items name = "object 1">
<something1>something1</something1>
</Items>
</body>
</Ratings>
</rss>
Input2.xml
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<Configs>
<Config name="test3">
<title>Title 3</title>
<author>Author3</author>
</Config>
<Config name="test4">
<title>Title 4</title>
<author>Author4</author>
</Config>
</Configs>
<optional>I dont want this to be copied</optional>
<Ratings>
<body>
<Items name = "object 2">
<something1>something2</something1>
</Items>
</body>
</Ratings>
</rss>
blank.xml(我将根据标签在其中插入元素的骨架xml)
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<Configs>
</Configs>
<Ratings>
<body>
</body>
</Ratings>
</rss>
这就是我要得到的
Result.xml
<rss version="2.0">
<Configs>
<Config name="test1">
<title>Title 1</title>
<author>Author1</author>
</Config>
<Config name="test2">
<title>Title 2</title>
<author>Author2</author>
</Config>
</Configs>
<optional>I dont want this to be copied</optional>
<Ratings>
<body>
<Items name="object 1">
<something1>something1</something1>
</Items>
</body>
</Ratings>
<Configs>
</Configs>
</rss>
这就是我需要的
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<Configs>
<Config name="test1">
<title>Title 1</title>
<author>Author1</author>
</Config>
<Config name="test2">
<title>Title 2</title>
<author>Author2</author>
</Config>
<Config name="test3">
<title>Title 3</title>
<author>Author3</author>
</Config>
<Config name="test4">
<title>Title 4</title>
<author>Author4</author>
</Config>
</Configs>
<Ratings>
<body>
<Items name="object 1">
<something1>something1</something1>
</Items>
<Items name="object 2">
<something1>something2</something1>
</Items>
</body>
</Ratings>
</rss>
更新 这就是我在标记之间没有更新值的情况。
这就是我将项目定义为NodeList的方式
NodeList itemsNodeList = inputDoc.getElementsByTagName("Items");
<rss version="2.0">
<Configs>
<Config name="test1">
<title>Title 1</title>
<author>Author1</author>
</Config>
<Config name="test2">
<title>Title 2</title>
<author>Author2</author>
</Config>
<Config name="test3">
<title>Title 3</title>
<author>Author3</author>
</Config>
<Config name="test4">
<title>Title 4</title>
<author>Author4</author>
</Config>
</Configs>
<Ratings>
<body>
<Items name="object 1">
<something1>something1</something1>
</Items>
<Items name="object 2">
<something1>something2</something1>
</Items>
</body>
</Ratings>
</rss>
答案 0 :(得分:2)
Java解决方案:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
File file = new File("Result.xml");
String baseXml = "blank.xml";
String[] inputXmls = {"input1.xml","input2.xml"};
Document resultDoc = db.parse(baseXml);
Node resultConfigsNode = resultDoc.getElementsByTagName("Configs").item(0);
Node resultRatingsBodyNode = resultDoc.getElementsByTagName("body").item(0);
for (String inputXml : inputXmls){
Document inputDoc = db.parse(inputXml);
NodeList configNodeList = inputDoc.getElementsByTagName("Config");
for (int i = 0; i < configNodeList.getLength(); i++) {
Node copiedNode = resultDoc.importNode(configNodeList.item(i), true);
resultConfigsNode.appendChild(copiedNode);
}
for (int i = 0; i < itemsNodeList.getLength(); i++) {
Node copiedNode = resultDoc.importNode(itemsNodeList.item(i), true);
NamedNodeMap attrMap = copiedNode.getAttributes();
Node n = attrMap.getNamedItem("name");
if(n.getNodeValue().equals("object 1")){
System.out.println("Items object 1");
}
resultRatingsBodyNode.appendChild(copiedNode);
}
NodeList valueNodeList = inputDoc.getElementsByTagName("value");
for (int i = 0; i < valueNodeList.getLength(); i++) {
Node copiedNode = resultDoc.importNode(valueNodeList.item(i), true);
Text txt = (Text) copiedNode.getFirstChild();
txt.setData("NewValue");
resultRatingsBodyNode.appendChild(copiedNode);
}
}
processXml(resultDoc, file);
答案 1 :(得分:1)
使用XSLT进行此类工作,将使您的生活更加轻松(可以从Java轻松调用XSLT)。使用blank.xml作为主要输入文档,可以在XSLT 3.0中做到这一点(使用1.0或2.0并不困难):
<xsl:transform version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:mode on-no-match="shallow-copy"/>
<xsl:param name="input1" select="doc('input1.xml')"/>
<xsl:param name="input2" select="doc('input2.xml')"/>
<xsl:template match="Configs">
<xsl:copy>
<xsl:copy-of select="$input1//Config, $input2//Config"/>
</xsl:copy>
</xsl:template>
<xsl:template match="body">
<xsl:copy>
<xsl:copy-of select="$input1//Items, $input2//Items"/>
</xsl:copy>
</xsl:template>
</xsl:transform>