我在网上搜索,我在 stackoverflow 上下搜索。没有解决方案。虽然我找到了如何在纯 xslt 中执行此操作的解决方案 here .
但问题是生成的 xml 将有数百 MB 大。所以我必须用 Java 中的 SAX 来做到这一点。 (请不要使用 xslt 解决方案,尽管我用 xslt 标记了它;-))
让我更详细地解释一下。我有几个应该解析的多个 xml 文件(最好是 InputSteam 的)。文件或 InputStream 看起来像
输入流1
<root>
<doc>
<tag>test1</tag>
</doc>
<doc>
<tag>test2</tag>
</doc>
...
</root>
输入流2
<root>
<doc>
<tag>test3</tag>
</doc>
<doc>
<tag>test4</tag>
</doc>
...
</root>
inputstream1+inputstream2+...+inputstreamN = 生成的 xml。看起来像
<root>
<doc>
<tag>test1</tag>
</doc>
<doc>
<tag>test2</tag>
</doc>
...
<doc>
<tag>test3</tag>
</doc>
<doc>
<tag>test4</tag>
</doc>
...
</root>
有人对此有解决方案或链接吗?这可能通过实现自定义 InputSource 还是我应该使用自定义 ContentHandler?或者这可能是 joost/stx ?
如果我可以使用 ContentHandler 的好处是我可以应用一些小的转换(我已经实现了这个)。但问题是我不知道如何将多个文件或 InputStream 作为 InputSource 传递:
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(customHandler);
xmlReader.parse(getInputSource()); // only one InputStream possible
还是应该直接在我的 ContentHandler 中解析 InputStreams?
最佳答案
我自己没有这样做过,但我记得看到一篇 IBM developerworks 文章看起来很容易做到这一点。
现在有点旧了,但是试试http://www.ibm.com/developerworks/xml/library/x-tipstx5/index.html
这是 StAX 而不是 SAX。我不确定当前的 JDK 是否包含 StAX。如果没有,您可能可以从 http://stax.codehaus.org/ 获得它
关于java - 高效地将多个大型 xml 文件合并为一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2275860/