使用STaX解析多个XML片段 [英] Parsing multiple XML fragments with STaX

查看:187
本文介绍了使用STaX解析多个XML片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以下内容可以在StAX中解析,

I was hoping the following would be parseable in 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, e.g. 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?

推荐答案

Woodstox StAX实现显然支持这一点: http://woodstox.codehaus.org/3.2。 9 / javadoc / com / ctc / wstx / api / WstxInputProperties.html#P_INPUT_PARSING_MODE

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,但是我没想到谷歌特定于Woodstox的选项!

As it happens we are already using Woodstox in some places, but I didn't think to Google for Woodstox-specific options!

这篇关于使用STaX解析多个XML片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆