Stax 将 Text+CDATA+Text 视为单个 CHARACTERS 部分
使用 Stax,我惊讶地发现诸如以下的 XML 块
<badger>
<![CDATA[Text about a badger]]>
</badger>
被视为:
START_ELEMENT (badger)
CHARACTERS ( Text about a badger )
END_ELEMENT (badger)
也就是说,CDATA 和周围的文本被平展为一个文本元素。未检测到 CDATA 元素。
这是正确的行为吗?如何将空格与 CDATA 分开?
我正在使用 woodstox 实现。
Using Stax, I'm surprised to find that an XML 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我怀疑您将属性“XMLInputFactory.IS_COALECING”设置为 true(或者,正在使用默认启用它的 Woodstox 3.2——这不是默认的 stax 规范建议的,即是一个小错误)。这会强制将 CDATA 转换为 CHARACTERS,并合并相邻的文本段(如果有)。
除此之外,Woodstox 确实将 CDATA 部分报告为不同的;但是 Stax 规范对转换有一些“有趣”的要求——专家组成员似乎不喜欢 CDATA 的处理方式与 CHARACTERS 的处理方式有任何不同。
因此:如果您确实想让它们单独报告,请确保禁用 IS_COALECING:
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:
CDATA 不是一个元素;而是一个元素。它是一种转义机制,告诉 XML 解析器不要费心寻找该部分中的嵌套标签。这对于包含诸如 < 之类的字符的文本很有用。和 &,以避免繁琐地单独转义它们,或者因为有其他原因导致正常的转义序列不起作用。
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.
我不知道 woodstox 的实现,但这个错误可以在 2006 年解决,仍然是一个因素吗? 您是否正在设置可选的 report-cdata-event 属性?
(另请参阅关于类似问题的此消息 .)
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.)