Reputation: 165
I am trying to merge multiple xml into a skeleton xml file to create a new XML File. I am trying to create a loop to include multiple xmls and merge into one xml. I am trying something like this but it is not working :File dir = new File("E:\temp\"); File[] rootFiles = dir.listFiles(); So far I am not able to create correct xml with single XML. Any help would be appreciated.
Here is my code with single xml input :
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 Files
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 (skeleton xml where I will be inserting the elements according to the tags)
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<Configs>
</Configs>
<Ratings>
<body>
</body>
</Ratings>
</rss>
This is what i am getting
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>
This is what I need
<?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>
Update This is what I got without the updated value between the tag.
This is how I defined the Items as 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>
Upvotes: 1
Views: 1909
Reputation: 255
Java solution:
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);
Upvotes: 2
Reputation: 163675
Use XSLT for this kind of job, it will make your life much easier (XSLT can be invoked easily from Java). With blank.xml as your principal input document, it can be done like this in XSLT 3.0 (and isn't much harder using 1.0 or 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>
Upvotes: 1