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
Moving Items Around in an Arraylist
Encrypt and Decrypt with Aes and Base64 Encoding
How to Create a Hashmap with Two Keys (Key-Pair, Value)
Jackson: How to Prevent Field Serialization
When Are Java Strings Interned
Differences Between Java 8 Date Time API (Java.Time) and Joda-Time
Convert String to Int Array in Java
Check Line for Unprintable Characters While Reading Text File
Where Do I Have to Place the Jdbc Driver for Tomcat's Connection Pool
Java.Util.Date to Xmlgregoriancalendar
Can't a Swing Component Be Added to Multiple Containers
Counting the Number of Files in a Directory Using Java
How to Check for a Valid Url in Java
How to Update an Entity Using Spring-Data-Jpa
What's the Difference Between Instance Method Reference Types in Java 8
How to Copy File Inside Jar to Outside the Jar