title: Xml Merge Multiple Xmls into a new Xml file
body: <p>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.</p>
  
  <p>Here is my code with single xml input :</p>
  
  <pre><code> 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);
  }
  </code></pre>
  
  <p>XML Files</p>
  
  <p>Input1.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;rss version="2.0"&gt;
      &lt;Configs&gt;
          &lt;Config name="test1"&gt;
              &lt;title&gt;Title 1&lt;/title&gt;
              &lt;author&gt;Author1&lt;/author&gt;
          &lt;/Config&gt;
          &lt;Config name="test2"&gt;
              &lt;title&gt;Title 2&lt;/title&gt;
              &lt;author&gt;Author2&lt;/author&gt;
          &lt;/Config&gt;
      &lt;/Configs&gt;
      &lt;optional&gt;I dont want this to be copied&lt;/optional&gt;
      &lt;Ratings&gt;
          &lt;body&gt;
              &lt;Items name = "object 1"&gt;
                  &lt;something1&gt;something1&lt;/something1&gt;
              &lt;/Items&gt;
          &lt;/body&gt;
      &lt;/Ratings&gt;
  &lt;/rss&gt;
  </code></pre>
  
  <p>Input2.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;rss version="2.0"&gt;
      &lt;Configs&gt;
          &lt;Config name="test3"&gt;
              &lt;title&gt;Title 3&lt;/title&gt;
              &lt;author&gt;Author3&lt;/author&gt;
          &lt;/Config&gt;
          &lt;Config name="test4"&gt;
              &lt;title&gt;Title 4&lt;/title&gt;
              &lt;author&gt;Author4&lt;/author&gt;
          &lt;/Config&gt;
      &lt;/Configs&gt;
      &lt;optional&gt;I dont want this to be copied&lt;/optional&gt;
      &lt;Ratings&gt;
          &lt;body&gt;
              &lt;Items name = "object 2"&gt;
                  &lt;something1&gt;something2&lt;/something1&gt;
              &lt;/Items&gt;
          &lt;/body&gt;
      &lt;/Ratings&gt;
  &lt;/rss&gt;
  </code></pre>
  
  <p>blank.xml (skeleton xml where I will be inserting the elements according to the tags)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;rss version="2.0"&gt;
      &lt;Configs&gt;
  
      &lt;/Configs&gt;
      &lt;Ratings&gt;
          &lt;body&gt;
  
          &lt;/body&gt;
      &lt;/Ratings&gt;
  &lt;/rss&gt;
  </code></pre>
  
  <p>This is what i am getting</p>
  
  <p>Result.xml</p>
  
  <pre><code>&lt;rss version="2.0"&gt;
      &lt;Configs&gt;
          &lt;Config name="test1"&gt;
              &lt;title&gt;Title 1&lt;/title&gt;
              &lt;author&gt;Author1&lt;/author&gt;
          &lt;/Config&gt;
          &lt;Config name="test2"&gt;
              &lt;title&gt;Title 2&lt;/title&gt;
              &lt;author&gt;Author2&lt;/author&gt;
          &lt;/Config&gt;
      &lt;/Configs&gt;
      &lt;optional&gt;I dont want this to be copied&lt;/optional&gt;
      &lt;Ratings&gt;
          &lt;body&gt;
              &lt;Items name="object 1"&gt;
                  &lt;something1&gt;something1&lt;/something1&gt;
              &lt;/Items&gt;
          &lt;/body&gt;
      &lt;/Ratings&gt;
  &lt;Configs&gt;
  
      &lt;/Configs&gt;
  &lt;/rss&gt;
  </code></pre>
  
  <p>This is what I need</p>
  
  <pre><code> &lt;?xml version="1.0" encoding="utf-8" ?&gt;
      &lt;rss version="2.0"&gt;
      &lt;Configs&gt;
              &lt;Config name="test1"&gt;
                  &lt;title&gt;Title 1&lt;/title&gt;
                  &lt;author&gt;Author1&lt;/author&gt;
              &lt;/Config&gt;
              &lt;Config name="test2"&gt;
                  &lt;title&gt;Title 2&lt;/title&gt;
                  &lt;author&gt;Author2&lt;/author&gt;
              &lt;/Config&gt;
              &lt;Config name="test3"&gt;
                  &lt;title&gt;Title 3&lt;/title&gt;
                  &lt;author&gt;Author3&lt;/author&gt;
              &lt;/Config&gt;
              &lt;Config name="test4"&gt;
                  &lt;title&gt;Title 4&lt;/title&gt;
                  &lt;author&gt;Author4&lt;/author&gt;
              &lt;/Config&gt;
          &lt;/Configs&gt;
          &lt;Ratings&gt;
              &lt;body&gt;
                  &lt;Items name="object 1"&gt;
                      &lt;something1&gt;something1&lt;/something1&gt;
                  &lt;/Items&gt;
                  &lt;Items name="object 2"&gt;
                      &lt;something1&gt;something2&lt;/something1&gt;
                  &lt;/Items&gt;
              &lt;/body&gt;
          &lt;/Ratings&gt;
      &lt;/rss&gt;
  </code></pre>
  
  <p><strong>Update</strong>
  This is what I got without the updated value between the  tag.</p>
  
  <p>This is how I defined the Items as NodeList</p>
  
  <pre><code>NodeList itemsNodeList = inputDoc.getElementsByTagName("Items");
  
  
  &lt;rss version="2.0"&gt;
      &lt;Configs&gt;
  
      &lt;Config name="test1"&gt;
              &lt;title&gt;Title 1&lt;/title&gt;
              &lt;author&gt;Author1&lt;/author&gt;
          &lt;/Config&gt;
  &lt;Config name="test2"&gt;
              &lt;title&gt;Title 2&lt;/title&gt;
              &lt;author&gt;Author2&lt;/author&gt;
          &lt;/Config&gt;
  &lt;Config name="test3"&gt;
              &lt;title&gt;Title 3&lt;/title&gt;
              &lt;author&gt;Author3&lt;/author&gt;
          &lt;/Config&gt;
  &lt;Config name="test4"&gt;
              &lt;title&gt;Title 4&lt;/title&gt;
              &lt;author&gt;Author4&lt;/author&gt;
          &lt;/Config&gt;
  &lt;/Configs&gt;
      &lt;Ratings&gt;
          &lt;body&gt;
  
          &lt;Items name="object 1"&gt;
                  &lt;something1&gt;something1&lt;/something1&gt;
              &lt;/Items&gt;
  &lt;Items name="object 2"&gt;
                  &lt;something1&gt;something2&lt;/something1&gt;
              &lt;/Items&gt;
  &lt;/body&gt;
      &lt;/Ratings&gt;
  &lt;/rss&gt;
  </code></pre>