Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <ol> <li>Iterate over all the node_id elements in file2. </li> <li>For each one, look up corresponding node_x element in file1. </li> <li>Add node_x from file1 into file2</li> </ol> <p>The following code illustrates this:</p> <pre><code>DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); //build DOMs Document doc1 = builder.parse(new File("file1.xml")); Document doc2 = builder.parse(new File("file2.xml")); //get all node_ids from doc2 and iterate NodeList list = doc2.getElementsByTagName("node_id"); for(int i = 0 ; i&lt; list.getLength() ; i++){ Node n = list.item(i); //extract the id String id = n.getTextContent(); //now get all node_id elements from doc1 NodeList list2 = doc1.getElementsByTagName("node_"+id); for(int j = 0 ; j&lt; list2.getLength() ; j++){ Node m = list2.item(j); //import them into doc2 Node imp = doc2.importNode(m,true); n.getParent().appendChild(imp); } } //write out the modified document to a new file TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); Source source = new DOMSource(doc2); Result output = new StreamResult(new File("merged.xml")); transformer.transform(source, output); </code></pre> <p>The result would be: </p> <pre><code>&lt;root&gt; &lt;node&gt; &lt;node_id&gt;1&lt;/node_id&gt; &lt;node_1&gt;This is 1&lt;/node_1&gt; &lt;/node&gt; &lt;node&gt; &lt;node_id&gt;2&lt;/node_id&gt; &lt;node_2&gt;This is 2&lt;/node_2&gt; &lt;/node&gt; &lt;node&gt; &lt;node_id&gt;3&lt;/node_id&gt; &lt;node_3&gt;This is 3&lt;/node_3&gt; &lt;/node&gt; &lt;/root&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload