How to merge >1000 xml files into one in Java

talos1990

New Member
I am trying to merge many xml files into one. I have successfully done that in DOM, but this solution is limited to a few files. When I run it on multiple files >1000 I am getting a java.lang.OutOfMemoryError.What I want to achieve is where i have the following filesfile 1:\[code\]<root>....</root>\[/code\]file 2:\[code\]<root>......</root>\[/code\]file n:\[code\]<root>....</root>\[/code\]resulting in:output:\[code\]<rootSet><root>....</root><root>....</root><root>....</root></rootSet>\[/code\]This is my current implementation:\[code\] DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element rootSetElement = doc.createElement("rootSet"); Node rootSetNode = doc.appendChild(rootSetElement); Element creationElement = doc.createElement("creationDate"); rootSetNode.appendChild(creationElement); creationElement.setTextContent(dateString); File dir = new File("/tmp/rootFiles"); String[] files = dir.list(); if (files == null) { System.out.println("No roots to merge!"); } else { Document rootDocument; for (int i=0; i<files.length; i++) { File filename = new File(dir+"/"+files); rootDocument = docBuilder.parse(filename); Node tempDoc = doc.importNode((Node) Document.getElementsByTagName("root").item(0), true); rootSetNode.appendChild(tempDoc); } } \[/code\]I have experimented a lot with xslt, sax, but I seem to keep missing something. Any help would be highly appreciated
 
Back
Top