使用 Stax Parse 合并多个文件不起作用

Merging Multiple Files Using Stax Parse Not Working

我们正在使用 Java 的 Java 8 和 XML 流 API 在单线程应用程序中合并两个 xml。

下面是我试图合并到最终 xml 的两个示例输入 xml。在下面两个示例中 xml html_tabs 是常见的 元素所以在最后的 XML 中必须只有一个 html_tabs 元素。

Sample XML 1
-----------------------
<?xml version='1.0' encoding='UTF-8'?>
<html_tabs lastRefreshDataDate="">
    <tab code="H_test_1" companyCode="H_test_1"
        id="H_test_1">
        <table>
            <tr><td>testing1 template</td></tr>
        </table>    
    </tab>  
</html_tabs>

Sample XML2
-----------------------
<?xml version='1.0' encoding='UTF-8'?>
<html_tabs lastRefreshDataDate="">
    <tab code="H_test_2" companyCode="H_test_2"
        id="H_test_2">
        <table>
            <tr><td>testing2 template</td></tr>
        </table>    
    </tab>  
</html_tabs>

Merged XML
---------------
<?xml version='1.0' encoding='UTF-8'?>
<html_tabs lastRefreshDataDate="">
 <tab code="H_test_1" companyCode="H_test_1"
        id="H_test_1">
        <table>
            <tr><td>testing1 template</td></tr>
        </table>    
 </tab>
 <tab code="H_test_2" companyCode="H_test_2"
        id="H_test_2">
        <table>
            <tr><td>testing2 template</td></tr>
        </table>    
 </tab> 
</html_tabs>

下面是我试图通过跳过 html_tabs 元素来合并两个 xml 的代码。我跳过了导入语句以使问题简短。

public class MergeXML1 {

    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        XMLEventWriter eventWriter;
        XMLEventFactory eventFactory;
        XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
        XMLInputFactory inputFactory = XMLInputFactory.newInstance();
        
        eventWriter = outputFactory.createXMLEventWriter(new FileOutputStream("testMerge1.xml"));
        eventFactory = XMLEventFactory.newInstance();
    
        // Create and write Start Tag
        XMLEvent statAbEvent = eventFactory.createStartDocument();
        eventWriter.add(statAbEvent);
        statAbEvent = eventFactory.createStartElement(StringUtils.EMPTY, StringUtils.EMPTY, "html_tabs");
        eventWriter.add(statAbEvent);
        statAbEvent = eventFactory.createAttribute("lastRefreshDataDate", StringUtils.EMPTY);
        eventWriter.add(statAbEvent);
        String[] filenames = new String[]{"4102530_1_statabfinsum.xml","4102530_2_statabfinsum.xml"};
        
        for (String filename : filenames) {
            XMLEventReader test = inputFactory.createXMLEventReader(filename, new FileInputStream(filename));
            System.out.println("read the first file : " + filename);
            
            while (test.hasNext()) {
                XMLEvent event = test.nextEvent();

                if (event.getEventType() != XMLEvent.START_DOCUMENT && event.getEventType() != XMLEvent.END_DOCUMENT) {
                    if (event.isStartElement()) {
                        StartElement startElement = event.asStartElement();
                        System.out.println("element name:" + startElement.getName().getLocalPart());
                        
                        if (!startElement.getName().getLocalPart().equals("html_tabs")) {
                            eventWriter.add(event);
                            continue;
                        } else {
                            continue;
                        }
                    }
                    eventWriter.add(event);
                }
            }
            
            test.close();
        }
        eventWriter.add(eventFactory.createEndElement("", "", "html_tabs"));
        eventWriter.add(eventFactory.createEndDocument());
        eventWriter.close();

    }

}

在尝试合并两个文件时使用上面的源代码会抛出异常。

Exception in thread "main" javax.xml.stream.XMLStreamException: Trying to output second root, <tab>
    at com.ctc.wstx.sw.BaseStreamWriter.throwOutputError(BaseStreamWriter.java:1589)
    at com.ctc.wstx.sw.BaseStreamWriter.throwOutputError(BaseStreamWriter.java:1596)
    at com.ctc.wstx.sw.BaseStreamWriter.reportNwfStructure(BaseStreamWriter.java:1624)
    at com.ctc.wstx.sw.BaseNsStreamWriter.checkStartElement(BaseNsStreamWriter.java:483)
    at com.ctc.wstx.sw.SimpleNsStreamWriter.writeStartOrEmpty(SimpleNsStreamWriter.java:257)
    at com.ctc.wstx.sw.BaseNsStreamWriter.writeStartElement(BaseNsStreamWriter.java:325)
    at org.codehaus.stax2.ri.Stax2EventWriterImpl.add(Stax2EventWriterImpl.java:100)
    at com.citi.research.distribution.tasks.downloadtasks.MergeXML1.main(MergeXML1.java:60)

由于出现异常,我无法找出上面的代码有什么问题。

根据检查您的代码,您似乎将最终的 EndElement 从第一个文件复制到输出,使您回到顶层。然后,当您尝试从第二个文件写入(正确的)StartElement 时,您试图在根级别插入它。

从第一个文件中过滤掉(即不写入)EndElement