简体   繁体   English

需要一个CDATA事件通知stax解析器的java

[英]Need a CDATA event notifying stax parser for java

I have taken over the maintenance of an application that uses a stax parser to break down an XML file of many records into individual records for further processing. 我接管了一个使用stax解析器的应用程序的维护,将许多记录的XML文件分解为单个记录以供进一步处理。 Using this type of parser for this purpose is overkill in my opinion but I didn't write it. 为此目的使用这种类型的解析器在我看来是过度的,但我没有写它。

The application is now encountering data such as this: 该应用程序现在遇到如下数据:

<name><![CDATA[A & B]]></name>

Our current parser returns an event for the begin 'name' tag. 我们当前的解析器返回begin'name'标记的事件。 The next event is a character event with the value 'A & B'. 下一个事件是值为'A&B'的字符事件。

From Sun's web page I found this: 从Sun的网页上我发现了这个:


Reporting CDATA Events The javax.xml.stream.XMLStreamReader implemented in the Streaming XML Parser does not report CDATA events. 报告CDATA事件Streaming XML Parser中实现的javax.xml.stream.XMLStreamReader不报告CDATA事件。 If you have an application that needs to receive such events, configure the XMLInputFactory to set the following implementation-specific report-cdata-event property: 如果您的应用程序需要接收此类事件,请配置XMLInputFactory以设置以下特定于实现的report-cdata-event属性:

XMLInputFactory factory = XMLInptuFactory.newInstance();
factory.setProperty("report-cdata-event", Boolean.TRUE);

The parser we are using does not support the 'report-cdata-event' property. 我们使用的解析器不支持'report-cdata-event'属性。

I want to find a parser that will report such an event so I don't have to check every single piece of text for characters that need to be guarded by the CDATA construct. 我想找到一个报告这样一个事件的解析器,这样我就不必检查需要由CDATA构造保护的字符的每一段文本。

UPDATE: 更新:

After posting this I browsed some of the related questions and there was mention of the 'isCoalescing' property; 发布后我浏览了一些相关的问题,并提到'isCoalescing'属性; for the record it is being set to FALSE. 记录它被设置为FALSE。

The correct property is: " http://java.sun.com/xml/stream/properties/report-cdata-event ". 正确的属性是:“ http://java.sun.com/xml/stream/properties/report-cdata-event ”。 The property XMLInputFactory.IS_COALESCING must be set to false (default setting). 必须将属性XMLInputFactory.IS_COALESCING设置为false(默认设置)。

If these conditions are met, it works fine (tested with Oracle Java 7u51). 如果满足这些条件,它可以正常工作(使用Oracle Java 7u51测试)。

很确定Woodstox应该处理这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM