如何防止一对 XMLEvent[Reader|Writer] 将空标签拆分为开始和结束标签?
我对以下代码的工作原理有一个理解问题:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 XMLEvents 列表 似乎没有办法区分,它确实会生成
StartElement
和EndElement
事件。消费者需要针对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
andEndElement
event. The consumer would need to optimize for the case when aStartElement
is immediately followed by anEndElement
.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 likeXMLEventReader
implementation returned bycreateXMLEventReader
XMLEventReader
implementation to optimize this caseXMLInputFactory
and overridecreateXMLEventReader
to return an instance of yourXMLEventReader
subclassIf 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 methodwriteEmptyElement
.(Or you can give a try to my home-made pretty printer, it's based on
XMLStreamWriter
)除非我弄错了, 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.