Merging xml file using java NodeList
Asked Answered
A

3

8

I'm trying to merge two xml files as shown below but i can't able to get the desired output please help me thank you

Java code:

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");

File1.xml

    <company>
        <staff>
            <name>john</name>
            <phone>465456433</phone>
            <email>gmail1</email>
        </staff>
    </company>

File2.xml

    <company>
        <staff>
            <area>area1</area>
            <city>city1</city>
        </staff>
    </company>

Current output:

    <company>
        <staff>
            <name>john</name>
            <phone>465456433</phone>
            <email>gmail1</email>
        </staff>
        <staff>
            <area>area1</area>
            <city>city1</city>
        </staff>
    </company>

Expected Output:

    <company>
        <staff>
            <name>john</name>
            <phone>465456433</phone>
            <email>gmail1</email>
            <area>area1</area>
            <city>city1</city>
        </staff>
    </company>
Arleen answered 28/1, 2013 at 18:49 Comment(2)
See also: ibm.com/developerworks/xml/library/x-tipcombxslt and #1511188Bodiless
I believe your algorithm works only in case you have same count of subnodes of <staff> as in a for loop you iterate one NodeList, but use the same counter (i) for the other one.Cutting
A
7

In order to do that on your own. You should do this following :

public static void mergeXML(){


    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;
    Document doc = null;
    Document doc2 = null;

    try {
            db = dbf.newDocumentBuilder();
            doc = db.parse(new File("D:\\Loic_Workspace\\Test2\\res\\test.xml"));
            doc2 = db.parse(new File("D:\\Loic_Workspace\\Test2\\res\\test2.xml"));
            NodeList ndListFirstFile = doc.getElementsByTagName("staff");

            Node nodeArea = doc.importNode(doc2.getElementsByTagName("area").item(0), true);
            Node nodeCity = doc.importNode(doc2.getElementsByTagName("city").item(0), true);
            ndListFirstFile.item(0).appendChild(nodeArea);
            ndListFirstFile.item(0).appendChild(nodeCity);

          TransformerFactory tFactory = TransformerFactory.newInstance();
          Transformer transformer = tFactory.newTransformer();
          transformer.setOutputProperty(OutputKeys.INDENT, "yes");  

          DOMSource source = new DOMSource(doc);
          StreamResult result = new StreamResult(new StringWriter());
          transformer.transform(source, result); 

          Writer output = new BufferedWriter(new FileWriter("D:\\Loic_Workspace\\Test2\\res\\testFinal.xml"));
          String xmlOutput = result.getWriter().toString();  
          output.write(xmlOutput);
          output.close();

    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


}

Final output of testFinal.xml :

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<company>
 <staff>
 <name>john</name>
 <phone>465456433</phone>
 <email>gmail1</email>
 <area>area1</area>
<city>city1</city>
</staff>
 </company>

As you want it ;-)

Hope it helps,

Amply answered 3/2, 2013 at 21:25 Comment(1)
Strange, without Doccument.importNode appendChild will cause DOMExceptionAlexia
T
2

This solution is for files where you need to iterate and verify something before to merge.

file1.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <reactions>
        <reaction>
            <ID>07402</ID>
            <type>irreversible</type>
            <substrate>15666</substrate>
            <product>07756</product>
        </reaction>
        <reaction>
    <ID>03063</ID>
    <type>irreversible</type>
    <substrate>00916</substrate>
    <product>04712</product>
</reaction>

file2.xml:

    <?xml version="1.0" encoding="UTF-8"?><reactions>
    <reaction>
        <ID>00001</ID>
        <reactionName>polyphosphate polyphosphohydrolase</reactionName>
        <reactionDescription> Polyphosphate + n H2O &lt;=&gt; (n+1) Oligophosphate</reactionDescription>
    </reaction>
    <reaction>
        <ID>00002</ID>
        <reactionName>Reduced ferredoxin:dinitrogen oxidoreductase (ATP-hydrolysing)</reactionName>
        <reactionDescription> 16 ATP + 16 H2O + 8 Reduced ferredoxin &lt;=&gt; 8 e- + 16 Orthophosphate + 16 ADP + 8 Oxidized ferredoxin</reactionDescription>
    </reaction>    
    <reaction>
        <ID>03063</ID>
        <reactionName>cephalosporin-C:2-oxoglutarate aminotransferase</reactionName>
        <reactionDescription> Cephalosporin C + 2-Oxoglutarate &lt;=&gt; (7R)-7-(5-Carboxy-5-oxopentanoyl)aminocephalosporinate + D-Glutamate</reactionDescription>
    </reaction>
    <reaction>
        <ID>07402</ID>
        <reactionName>(7R)-7-(4-carboxybutanamido)cephalosporanate amidohydrolase</reactionName>
        <reactionDescription> (7R)-7-(4-Carboxybutanamido)cephalosporanate + H2O &lt;=&gt; 7-Aminocephalosporanic acid + Glutarate</reactionDescription>
    </reaction>  
</reactions>

Result.xml

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<reactions>
    <reaction>
        <ID>07402</ID>
        <type>irreversible</type>
        <substrate>15666</substrate>
        <product>07756</product>
    <reactionName>(7R)-7-(4-carboxybutanamido)cephalosporanate amidohydrolase</reactionName>
<reactionDescription> (7R)-7-(4-Carboxybutanamido)cephalosporanate + H2O &lt;=&gt; 7-Aminocephalosporanic acid + Glutarate</reactionDescription>
</reaction>
    <reaction>
        <ID>03063</ID>
        <type>irreversible</type>
        <substrate>00916</substrate>
        <product>04712</product>
    <reactionName>cephalosporin-C:2-oxoglutarate aminotransferase</reactionName>
<reactionDescription> Cephalosporin C + 2-Oxoglutarate &lt;=&gt; (7R)-7-(5-Carboxy-5-oxopentanoyl)aminocephalosporinate + D-Glutamate</reactionDescription>
</reaction>
</reactions>

Java program to do this:

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;
import java.io.Writer;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class MergeXML {

    public static void main(String[] args) {
        MergeXML m = new MergeXML();
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            DocumentBuilder db;

            db = dbf.newDocumentBuilder();

            Document secondaryMetabolismXML = db
                    .parse(new File("/home/bioinfo/workspace/teste/src/file1.xml"));
            Document generalMetabolismXML = db
                    .parse(new File("/home/bioinfo/workspace/teste/src/file2.xml"));

            NodeList secondaryReactions = secondaryMetabolismXML.getElementsByTagName("reaction");
            NodeList generalReactions = generalMetabolismXML.getElementsByTagName("reaction");

            for (int s = 0; s < secondaryReactions.getLength(); s++) {
                Node secondaryReaction = secondaryReactions.item(s);
                for (int g = 0; g < generalReactions.getLength(); g++) {
                    Node generalReaction = generalReactions.item(g);
                    if (getChildrenByNodeName(secondaryReaction, "ID").getTextContent()
                            .equals(getChildrenByNodeName(generalReaction, "ID").getTextContent())) {
                        if (getChildrenByNodeName(generalReaction, "reactionName") != null) {
                            secondaryReaction.appendChild(secondaryMetabolismXML
                                    .importNode(getChildrenByNodeName(generalReaction, "reactionName"), true));
                        }
                        if (getChildrenByNodeName(generalReaction, "reactionAlternativeName") != null) {
                            secondaryReaction.appendChild(secondaryMetabolismXML.importNode(
                                    getChildrenByNodeName(generalReaction, "reactionAlternativeName"), true));
                        }
                        if (getChildrenByNodeName(generalReaction, "reactionDescription") != null) {
                            secondaryReaction.appendChild(secondaryMetabolismXML
                                    .importNode(getChildrenByNodeName(generalReaction, "reactionDescription"), true));
                        }
                    }
                }
            }
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");

            DOMSource source = new DOMSource(secondaryMetabolismXML);
            StreamResult result = new StreamResult(new StringWriter());
            transformer.transform(source, result);

            Writer output = new BufferedWriter(
                    new FileWriter("/home/bioinfo/workspace/teste/src/Result.xml"));
            String xmlOutput = result.getWriter().toString();
            output.write(xmlOutput);
            output.close();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Returns a node child when you have a match with a given node name
     * 
     * @param node
     * @param nodeName
     * @return
     */
    public static Node getChildrenByNodeName(Node node, String nodeName) {
        for (Node childNode = node.getFirstChild(); childNode != null;) {
            Node nextChild = childNode.getNextSibling();
            if (childNode.getNodeName().equalsIgnoreCase(nodeName)) {
                return childNode;
            }
            childNode = nextChild;
        }
        return null;
    }
}
Tsui answered 26/4, 2016 at 13:29 Comment(0)
C
0

Problem is, you want to append child elements to "staff" element but what you actually do is :

nodes.item(i).getParentNode().appendChild(n);

meaning you're looking for the parent node of one of the "staff" nodes of the list, and that node is "company". So you're appending a new "staff" node (the one imported from doc1) to the "company" node of doc

Now, what you want to do is to iterate over "staff" child nodes of doc1 and to append them one by one to the "staff" node of doc. So you would want to change nodes1 definition as following :

// Retrieving child nodes of first "staff" element of doc1
NodeList nodes1 = doc1.getElementsByTagName("staff").item(0).getChildNodes();

Then change the node you append that to by replacing

nodes.item(i).getParentNode().appendChild(n);

by

nodes.item(0).appendChild(n);

So now you'll be appending all "staff" child nodes (/!\ only for the first "staff" element) of doc1 to the first "staff" element of doc

Note 1 : Dont use the iteration variable (i) you use to run through list A to select an item of another list unless you're aware of what you're doing (for example both lists are of the same length)

Note 2 : That solution will append the nodes of first "staff" element of doc1 to the first "staff" element of doc. You will maybe want to add some iterations here and there.

Chase answered 4/2, 2013 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.