title: Merging multiple XML files in Java
body: <p>I've been looking for the best way to do this, but I can't seem to find a clear answer how this should be done.</p>
  
  <p>I have an Arraylist of Files in my Java code, representing a list of xml files which should be merged and written to a new XML file. This is not a fixed length list, I'm estimating it would be between 2-10 files. All these files have a very similar document structure, but some attributes should be summed while merging. For example:</p>
  
  <p>File1</p>
  
  <pre><code>&lt;events&gt;
    &lt;commandEvents date="2013-07-16"&gt;
      &lt;commandEvent count="1" commandId="update"/&gt;
      &lt;commandEvent count="1" commandId="debug"/&gt;
      &lt;commandEvent count="3" commandId="resume"/&gt;
    &lt;/commandEvents&gt;
  &lt;/events&gt;
  </code></pre>
  
  <p>File 2</p>
  
  <pre><code>&lt;events&gt;
    &lt;commandEvents date="2013-07-16"&gt;
      &lt;commandEvent count="2" commandId="resume"/&gt;
    &lt;/commandEvents&gt;
    &lt;commandEvents date="2013-07-15"&gt;
      &lt;commandEvent count="2" commandId="resume"/&gt;
      &lt;commandEvent count="1" commandId="update"/&gt;
    &lt;/commandEvents&gt;
  &lt;/events&gt;
  </code></pre>
  
  <p>Result</p>
  
  <pre><code>&lt;events&gt;
    &lt;commandEvents date="2013-07-16"&gt;
      &lt;commandEvent count="1" commandId="update"/&gt;
      &lt;commandEvent count="1" commandId="debug"/&gt;
      &lt;commandEvent count="5" commandId="resume"/&gt;
    &lt;/commandEvents&gt;
    &lt;commandEvents date="2013-07-15"&gt;
      &lt;commandEvent count="2" commandId="resume"/&gt;
      &lt;commandEvent count="1" commandId="update"/&gt;
    &lt;/commandEvents&gt;
  &lt;/events&gt;
  </code></pre>
  
  <p>To clarify, the merging should occur on commandEvents[@date]/commandEvent[@commandId].The commandEvent elements have some more attributes, but these are the same for each element so I've omitted them here. Not all dates will be available in each document.</p>
  
  <p>I first found some answers to go the XSLT route, but I'm quite confused about the XSLT syntax to do this. 
  Although I'm not entirely sure about the size that these files may reach, but I would be highly suprised they would be >1mb, so a Java DOM parser as JDOM or XOM might work as well, but I'd have to load all these files at the same time or iterate in pairs.</p>
  
  <p>What is regarded as the best way to do this? And if XSLT is regarded as the best solution, would it be possible to give me some tips for this?</p>