开发者

Stax considers Text+CDATA+Text to be a single CHARACTERS section

Using Stax, I'm surprised to find that an XML开发者_Go百科 block such as:

<badger>
    <![CDATA[Text about a badger]]>
</badger>

is treated as if it were:

START_ELEMENT (badger)
CHARACTERS (        Text about a badger    )
END_ELEMENT (badger)

That is, the CDATA and the surrounding text are flattened into one text element. There is no CDATA element detected.

Is this correct behaviour? How can I separate the whitespace from the CDATA?

I am using the woodstox implementation.


I suspect you have property 'XMLInputFactory.IS_COALESCING' set to true (or, are using Woodstox 3.2 that had it enabled by default -- which is not the default stax specs suggest, i.e. was a minor bug). This forces both conversion of CDATA into CHARACTERS, and coalesces adjacent text segments if any.

Other than this, Woodstox does report CDATA sections as distinct; but Stax specification has some 'interesting' requirements for convesion -- members of the expert group seemed to dislike idea of CDATA being handled any different from CHARACTERS.

So: if you do want to get them reported separate, make sure to disable IS_COALESCING:

inputFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);


CDATA isn't an element; it's an escape mechanism that tells the XML parser not to bother looking for nested tags within that section. This is useful for text that contains characters like < and &, to avoid tediously escaping them all individually, or because there's some other reason that normal escape sequences won't work.


I don't know about the woodstox implementation, but could this bug, resolved in 2006, still be a factor? Are you setting the optional report-cdata-event property?

(See also this message about a similar problem.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜