title: Merge Two XML Files in Java
body: <p>I have two XML files of similar structure which I wish to merge into one file.
  Currently I am using <a href="http://www.javaworld.com/javaworld/jw-07-2007/jw-07-xmlmerge.html?page=1" rel="noreferrer">EL4J XML Merge</a> which I came across in this tutorial. 
  However it does not merge as I expect it to for instances the main problem is its not merging the   from both files into one element aka one that contains 1, 2, 3 and 4. 
  Instead it just discards either 1 and 2 or 3 and 4 depending on which file is merged first.</p>
  
  <p>So I would be grateful to anyone who has experience with XML Merge if they could tell me what I might be doing wrong or alternatively does anyone know of a good XML API for Java that would be capable of merging the files as I require?</p>
  
  <p>Many Thanks for Your Help in Advance</p>
  
  <p>Edit:</p>
  
  <p>Could really do with some good suggestions on doing this so added a bounty. I've tried jdigital's suggestion but still having issues with XML merge.</p>
  
  <p>Below is a sample of the type of structure of XML files that I am trying to merge.</p>
  
  <pre><code>&lt;run xmloutputversion="1.02"&gt;
      &lt;info type="a" /&gt;
      &lt;debugging level="0" /&gt;
      &lt;host starttime="1237144741" endtime="1237144751"&gt;
          &lt;status state="up" reason="somereason"/&gt;
          &lt;something avalue="test" test="alpha" /&gt;
          &lt;target&gt;
              &lt;system name="computer" /&gt;
          &lt;/target&gt;
          &lt;results&gt;
              &lt;result id="1"&gt;
                  &lt;state value="test" /&gt;
                  &lt;service value="gamma" /&gt;
              &lt;/result&gt;
              &lt;result id="2"&gt;
                  &lt;state value="test4" /&gt;
                  &lt;service value="gamma4" /&gt;
              &lt;/result&gt;
          &lt;/results&gt;
          &lt;times something="0" /&gt;
      &lt;/host&gt;
      &lt;runstats&gt;
          &lt;finished time="1237144751" timestr="Sun Mar 15 19:19:11 2009"/&gt;
          &lt;result total="0" /&gt;
      &lt;/runstats&gt;
  &lt;/run&gt;
  
  &lt;run xmloutputversion="1.02"&gt;
      &lt;info type="b" /&gt;
      &lt;debugging level="0" /&gt;
      &lt;host starttime="1237144741" endtime="1237144751"&gt;
          &lt;status state="down" reason="somereason"/&gt;
          &lt;something avalue="test" test="alpha" /&gt;
          &lt;target&gt;
              &lt;system name="computer" /&gt;
          &lt;/target&gt;
          &lt;results&gt;
              &lt;result id="3"&gt;
                  &lt;state value="testagain" /&gt;
                  &lt;service value="gamma2" /&gt;
              &lt;/result&gt;
              &lt;result id="4"&gt;
                  &lt;state value="testagain4" /&gt;
                  &lt;service value="gamma4" /&gt;
              &lt;/result&gt;
          &lt;/results&gt;
          &lt;times something="0" /&gt;
      &lt;/host&gt;
      &lt;runstats&gt;
          &lt;finished time="1237144751" timestr="Sun Mar 15 19:19:11 2009"/&gt;
          &lt;result total="0" /&gt;
      &lt;/runstats&gt;
  &lt;/run&gt;
  </code></pre>
  
  <p>Expected output </p>
  
  <pre><code>&lt;run xmloutputversion="1.02"&gt;
      &lt;info type="a" /&gt;
      &lt;debugging level="0" /&gt;
      &lt;host starttime="1237144741" endtime="1237144751"&gt;
          &lt;status state="down" reason="somereason"/&gt;
          &lt;status state="up" reason="somereason"/&gt;
          &lt;something avalue="test" test="alpha" /&gt;
          &lt;target&gt;
              &lt;system name="computer" /&gt;
          &lt;/target&gt;
          &lt;results&gt;
              &lt;result id="1"&gt;
                  &lt;state value="test" /&gt;
                  &lt;service value="gamma" /&gt;
              &lt;/result&gt;
              &lt;result id="2"&gt;
                  &lt;state value="test4" /&gt;
                  &lt;service value="gamma4" /&gt;
              &lt;/result&gt;
              &lt;result id="3"&gt;
                  &lt;state value="testagain" /&gt;
                  &lt;service value="gamma2" /&gt;
              &lt;/result&gt;
              &lt;result id="4"&gt;
                  &lt;state value="testagain4" /&gt;
                  &lt;service value="gamma4" /&gt;
              &lt;/result&gt;
          &lt;/results&gt;
          &lt;times something="0" /&gt;
      &lt;/host&gt;
      &lt;runstats&gt;
          &lt;finished time="1237144751" timestr="Sun Mar 15 19:19:11 2009"/&gt;
          &lt;result total="0" /&gt;
      &lt;/runstats&gt;
  &lt;/run&gt;
  </code></pre>