nilesh nilesh - 5 months ago 18
Java Question

How to Merge two XMLs in Java

I am trying to merge two xmls in Java. I am using STaX API to write these XMLs. I searched a lot on internet on how to merge xmls but none seems as straight forward as C#. Is there any straight-forward way of doing this in Java using StAX? Probably xslt would not be the right solution since the file size can be big.

File1.xml

<TestCaseBlock>
<TestCase TestCaseID="1">
<Step ExecutionTime="2011-03-29 12:08:31 EST">
<Status>Passed</Status>
<Description>foo</Description>
<Expected>foo should pass</Expected>
<Actual>foo passed</Actual>
</Step>
</TestCase>
</TestCaseBlock>


File2.xml

<TestCaseBlock>
<TestCase TestCaseID="2">
<Step ExecutionTime="2011-03-29 12:08:32 EST">
<Status>Failed</Status>
<Description>test something</Description>
<Expected>something expected</Expected>
<Actual>not as expected</Actual>
</Step>
</TestCase>
</TestCaseBlock>


Merged.xml

<TestCaseBlock>
<TestCase TestCaseID="1">
<Step ExecutionTime="2011-03-29 12:08:33 EST">
<Status>Passed</Status>
<Description>foo</Description>
<Expected>foo should pass</Expected>
<Actual>foo passed</Actual>
</Step>
</TestCase>
<TestCase TestCaseID="2">
<Step ExecutionTime="2011-03-29 12:08:34 EST">
<Status>Failed</Status>
<Description>test something</Description>
<Expected>something expected</Expected>
<Actual>not as expected</Actual>
</Step>
</TestCase>
</TestCaseBlock>

Answer

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();