I was looking for the best way to do this, but I cannot find a clear answer on how this should be done.
I have an Arraylist of Files in my Java code representing a list of xml files to be combined and written to a new XML file. This is not a fixed list of lengths, I estimate that it will be between 2-10 files. All of these files have a very similar document structure, but some attributes should be summarized when merging. For instance:
File1
<events> <commandEvents date="2013-07-16"> <commandEvent count="1" commandId="update"/> <commandEvent count="1" commandId="debug"/> <commandEvent count="3" commandId="resume"/> </commandEvents> </events>
File 2
<events> <commandEvents date="2013-07-16"> <commandEvent count="2" commandId="resume"/> </commandEvents> <commandEvents date="2013-07-15"> <commandEvent count="2" commandId="resume"/> <commandEvent count="1" commandId="update"/> </commandEvents> </events>
Result
<events> <commandEvents date="2013-07-16"> <commandEvent count="1" commandId="update"/> <commandEvent count="1" commandId="debug"/> <commandEvent count="5" commandId="resume"/> </commandEvents> <commandEvents date="2013-07-15"> <commandEvent count="2" commandId="resume"/> <commandEvent count="1" commandId="update"/> </commandEvents> </events>
To clarify, merging should occur in commandEvents [@date] / commandEvent [@commandId]. The commandEvent elements have a few more attributes, but they are the same for each element, so I omitted them here. Not all dates will be available in every document.
At first I found some answers to go the XSLT path, but I'm pretty confused by the XSLT syntax for this. Although I'm not quite sure of the sizes these files can have, I would be very surprised that they will be> 1mb, so a Java DOM parser like JDOM or XOM can work, but I have to load all of these files into the same time or iteration in pairs.
What is considered the best way to do this? And if XSLT is considered the best solution, can you give me some tips on this?
source share