4

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?

Iain
  • 1,797
  • 1
  • 20
  • 38

3 Answers3

2

Nope. The StAX API does not support fragments. A XMLStreamReader is suitable for exactly one XML document. However, your "hack" isn't that bad at all...

chris
  • 3,573
  • 22
  • 20
  • Ok, thanks Chris.I guess whatever performance loss is quite acceptable in my context, I was just irritated to find I needed it. :-) – Iain Apr 02 '12 at 09:11
1

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

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

Iain
  • 1,797
  • 1
  • 20
  • 38
1

According to XML spec, an XML document must have a single root element, or else it isn't wellformed. So your so called hack isn't a hack at all, it is the best way to fix up the document....

vtd-xml-author
  • 3,319
  • 4
  • 22
  • 30