我正在使用GitHub中的代码https://github.com/koen-serneels/blog/tree/master/BigXmlTest/src/main/java/be/error/bigxmltest来分割大型XML文件(6GB)。我首先提供了用于理解程序的基本XML文件。程序输出分割的每个文件的标题。我现在也试图为每个文件添加一个页脚元素并遇到一些困难,任何有关如何进行此操作的建议或建议都会很棒。
我已将页脚添加到架构中并编写代码以在“openOutputFileAndWriteHeader()”方法中添加页脚。但是,这会在内容元素中添加页脚。如下所示:
private XMLEventWriter openOutputFileAndWriteHeader(int fileNumber) throws Exception
{
XMLEventFactory xmlEventFactory = XMLEventFactory.newInstance();
XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);
XMLEventWriter writer = xmlOutputFactory.createXMLEventWriter(new FileOutputStream(new File("C:\\Users\\thamm\\Desktop\\SplitFiles\\File_" + fileNumber + ".xml")));
writer.setDefaultNamespace(DOCUMENT_NS);
writer.add(xmlEventFactory.createStartDocument());
writer.add(xmlEventFactory.createStartElement(new QName(DOCUMENT_NS, ROOT_ELEMENT), null, null));
HeaderType header = objectFactory.createHeaderType();
header.setSomeHeaderElement("List of Employees");
marshaller.marshal(new JAXBElement<HeaderType>(new QName(DOCUMENT_NS, HEADER_ELEMENT, ""), HeaderType.class,
HeaderType.class, header), writer);
writer.add(new StartElementEvent(new QName(DOCUMENT_NS, CONTENT_ELEMENT)));
FooterType footer = objectFactory.createFooterType();
footer.setSomeFooterElement("Footer");
marshaller.marshal(new JAXBElement<FooterType>(new QName(DOCUMENT_NS, FOOTER_ELEMENT,""), FooterType.class,
FooterType.class, footer), writer);
return writer;
}
输出:
<?xml version='1.0' encoding='UTF-8'?>
<BigXmlTest xmlns="http://www.sidero.ie/bigxmltest">
<Header>
<SomeHeaderElement>List of Employees</SomeHeaderElement>
</Header>
<Content>
<Footer>
<SomeFooterElement>Footer</SomeFooterElement>
</Footer>
<Data>Data4</Data>
<Data>Data5</Data>
<Data>Data6</Data>
</Content>
</BigXmlTest>
答案 0 :(得分:0)
方法名称openOutputFileAndWriteHeader
暗示它将创建新文件。它不适合页脚。
在第74行的BigXmlTestIteratorApi.java中,您可以看到代码:
xmlEventWriter.close(); // Also closes any open Element(s) and the document
xmlEventWriter = openOutputFileAndWriteHeader(++fileNumber); // Continue with next file
dataRepetitions = 0;
要添加页脚,您需要在关闭文件前添加:
writeFooter(footer);
xmlEventWriter.close();
xmlEventWriter = openOutputFileAndWriteHeader(++fileNumber);
dataRepetitions = 0;
请注意,为每个文件创建FooterType
的实例可能是多余的。它可以在循环外部创建,例如在60行