Merge Two Xml Files in Java

Merge two XML files in java and make single xml

Yes its possible to merge xml files.
You can refer below links to merge your file. Do necessary changes in code of below links to achieve structure of your XML. If possible share structure of your XML , will help you with the relevant code.

Merge Two XML Files in Java

Merging xml file using java NodeList

How to Merge two XMLs in Java

I have a solution that works for me. Now experts, please advise if this is the way to go.

Thanks,
-Nilesh

    XMLEventWriter eventWriter;
XMLEventFactory eventFactory;
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream("testMerge1.xml"));
eventFactory = XMLEventFactory.newInstance();
XMLEvent newLine = eventFactory.createDTD("\n");
// Create and write Start Tag
StartDocument startDocument = eventFactory.createStartDocument();
eventWriter.add(startDocument);
eventWriter.add(newLine);
StartElement configStartElement = eventFactory.createStartElement("","","TestCaseBlock");
eventWriter.add(configStartElement);
eventWriter.add(newLine);
String[] filenames = new String[]{"test1.xml", "test2.xml","test3.xml"};
for(String filename:filenames){
XMLEventReader test = inputFactory.createXMLEventReader(filename,
new FileInputStream(filename));
while(test.hasNext()){
XMLEvent event= test.nextEvent();
//avoiding start(<?xml version="1.0"?>) and end of the documents;
if (event.getEventType()!= XMLEvent.START_DOCUMENT && event.getEventType() != XMLEvent.END_DOCUMENT)
eventWriter.add(event);
eventWriter.add(newLine);
test.close();
}
eventWriter.add(eventFactory.createEndElement("", "", "TestCaseBlock"));
eventWriter.add(newLine);
eventWriter.add(eventFactory.createEndDocument());
eventWriter.close();

How to merge 1000 xml files into one using Java

You might also consider using StAX. Here's code that would do what you want:

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;

import javax.xml.stream.XMLEventFactory;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLEventWriter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.stream.StreamSource;

public class XMLConcat {
public static void main(String[] args) throws Throwable {
File dir = new File("/tmp/rootFiles");
File[] rootFiles = dir.listFiles();

Writer outputWriter = new FileWriter("/tmp/mergedFile.xml");
XMLOutputFactory xmlOutFactory = XMLOutputFactory.newFactory();
XMLEventWriter xmlEventWriter = xmlOutFactory.createXMLEventWriter(outputWriter);
XMLEventFactory xmlEventFactory = XMLEventFactory.newFactory();

xmlEventWriter.add(xmlEventFactory.createStartDocument());
xmlEventWriter.add(xmlEventFactory.createStartElement("", null, "rootSet"));

XMLInputFactory xmlInFactory = XMLInputFactory.newFactory();
for (File rootFile : rootFiles) {
XMLEventReader xmlEventReader = xmlInFactory.createXMLEventReader(new StreamSource(rootFile));
XMLEvent event = xmlEventReader.nextEvent();
// Skip ahead in the input to the opening document element
while (event.getEventType() != XMLEvent.START_ELEMENT) {
event = xmlEventReader.nextEvent();
}

do {
xmlEventWriter.add(event);
event = xmlEventReader.nextEvent();
} while (event.getEventType() != XMLEvent.END_DOCUMENT);
xmlEventReader.close();
}

xmlEventWriter.add(xmlEventFactory.createEndElement("", null, "rootSet"));
xmlEventWriter.add(xmlEventFactory.createEndDocument());

xmlEventWriter.close();
outputWriter.close();
}
}

One minor caveat is that this API seems to mess with empty tags, changing <foo/> into <foo></foo>.

How to merge two XML into one using JAX-B?

I'm not sure why you would want to use JAXB for this, it seems over-complex.

You can do this with a one-line XQuery

<Root>{doc('A.xml')/*/*, doc('B.xml')/*/*}</Root>

Or with an only slightly longer XSLT:

<Root xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:copy-of select="document('A.xml')/*/* | document('B.xml')/*/*"/>
</Root>


Related Topics



Leave a reply



Submit