简体   繁体   English

使用STaX解析多个XML片段

[英]Parsing multiple XML fragments with STaX

I was hoping the following would be parseable in StAX, 我希望以下内容可以在StAX中解析,

<something a="b"/>
<something a="b"/>

But it chokes when you reach the second element. 但是当你到达第二个元素时它就会窒息。 As there is no common root element. 因为没有共同的根元素。 (I'm not too sure why a pull parser cares about this particular issue... anyway...) (我不太清楚为什么拉解析器会关心这个特定的问题......无论如何......)

I can fake a root element, eg Guava: 我可以伪造一个根元素,例如Guava:

    InputSupplier<Reader> join = CharStreams.join(
            newReaderSupplier("<root>"),
            newReaderSupplier(new File("...")),
            newReaderSupplier("</root>"));

    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xsr = xif.createXMLStreamReader(join.getInput());
    xsr.nextTag();  // Skip the fake root

So my question is just: Is there any way to avoid this hack? 所以我的问题是:有没有办法避免这种黑客行为? Some 'fragment' mode that I can put the parser into? 一些'片段'模式,我可以把解析器放入?

Nope. 不。 The StAX API does not support fragments. StAX API不支持片段。 A XMLStreamReader is suitable for exactly one XML document. XMLStreamReader仅适用于一个XML文档。 However, your "hack" isn't that bad at all... 但是,你的“黑客”并没有那么糟糕......

The Woodstox StAX implementation does apparently support this: http://woodstox.codehaus.org/3.2.9/javadoc/com/ctc/wstx/api/WstxInputProperties.html#P_INPUT_PARSING_MODE Woodstox StAX实现显然支持这一点: http ://woodstox.codehaus.org/3.2.9/javadoc/com/ctc/wstx/api/WstxInputProperties.html#P_INPUT_PARSING_MODE

As it happens we are already using Woodstox in some places, but I didn't think to Google for Woodstox-specific options! 碰巧我们已经在某些地方使用过Woodstox,但我没想到Google会选择特定于Woodstox的选项!

According to XML spec, an XML document must have a single root element, or else it isn't wellformed. 根据XML规范,XML文档必须具有单个根元素,否则它不能很好地形成。 So your so called hack isn't a hack at all, it is the best way to fix up the document.... 所以你所谓的hack根本就不是黑客,它是修复文档的最好方法....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM