如何防止一对 XMLEvent[Reader|Writer] 将空标签拆分为开始和结束标签?

发布于 2024-08-25 06:02:51 字数 670 浏览 6 评论 0原文

我对以下代码的工作原理有一个理解问题:

XMLInputFactory xif = XMLInputFactory.newFactory();
XMLOutputFactory xof = XMLOutputFactory.newFactory();

XMLEventReader reader = xif.createXMLEventReader(/* ... */);
XMLEventWriter writer = xof.createXMLEventWriter(/* ... */);

writer.add(reader);

方法 writer.add([some reader]) 从 reader 读取所有事件并随后写入它们。遗憾的是,发生了以下情况:

输入

<root><c/></root>

被转换为

<root><c><c/></root>

我知道,从 XML 的角度来看,这些是相同的树,但对于人类来说不是;)

我该怎么做才能获得相同的输出?

仅供参考:稍后我需要一个 XMLEvent[Reader|Writer] 对来过滤“XML 事件”。

I have an understanding problem of how the following code works:

XMLInputFactory xif = XMLInputFactory.newFactory();
XMLOutputFactory xof = XMLOutputFactory.newFactory();

XMLEventReader reader = xif.createXMLEventReader(/* ... */);
XMLEventWriter writer = xof.createXMLEventWriter(/* ... */);

writer.add(reader);

The method writer.add([some reader]) reads all events from reader and writes them consequently. Sadly, following happens:

The input

<root><c/></root>

gets transformed to

<root><c><c/></root>

I know, from XML point of view, these are equal trees, but not for a human ;)

What can I do to get the same output?

FYI: I need a XMLEvent[Reader|Writer] pair later to filter "XML events".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

°如果伤别离去 2024-09-01 06:02:51

根据 XMLEvents 列表 似乎没有办法区分,它确实会生成 StartElementEndElement 事件。消费者需要针对 StartElement 后紧跟着 EndElement 的情况进行优化。

工厂返回的 XMLEventReader 显然不是这种情况。 实现是什么之外,我别无选择。

  • 如果您想自己优化此行为,我认为除了检查 createXMLEventReader 子类 XMLEventReader 返回的具体
  • XMLEventReader code> 实现来优化此案
  • 例子类 XMLInputFactory 并重写 createXMLEventReader 以返回 XMLEventReader 子类的实例

如果这听起来太复杂(或不'不工作),我建议您使用 使用 的解决方案XMLStreamWriter。这个有一个专用的方法writeEmptyElement

(或者你可以尝试一下我的自制的漂亮打印机,它基于XMLStreamWriter)

According to the list of XMLEvents it seems like there is no way to make the distinction and it will indeed generate a StartElement and EndElement event. The consumer would need to optimize for the case when a StartElement is immediately followed by an EndElement.

This is apparently not the case of the XMLEventReader returned by the factory. If you want to optimize this behavior yourself, I see no other way than to do something like

  • check what's the concrete XMLEventReader implementation returned by createXMLEventReader
  • subclass the XMLEventReader implementation to optimize this case
  • subclass XMLInputFactory and override createXMLEventReader to return an instance of your XMLEventReader subclass

If this sounds too complicated (or doesn't work), I would suggest that you go with a solution that uses XMLStreamWriter. This one has a dedicated method writeEmptyElement.

(Or you can give a try to my home-made pretty printer, it's based on XMLStreamWriter)

烟花易冷人易散 2024-09-01 06:02:51

除非我弄错了, Woodstox 可以配置为生成空标签(默认)或不生成空标签(所以它'即使没有内容,也会始终输出开始+结束标记)。
由于您可以为 XMLStreamWriter 配置此功能,然后可以使用该流编写器生成 XMLEventWriter,因此它应该按预期工作。

Unless I am mistaken, Woodstox can be configured to either produce empty tags (default), or not (so it'll always output start+end tag, even if there is no content).
Since you can configure this for XMLStreamWriter, and you can then produce XMLEventWriter using that stream writer, it should work as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文