Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. The next event is a character event with the value 'A & B'.

From Sun's web page I found this:


Reporting CDATA Events The javax.xml.stream.XMLStreamReader implemented in the Streaming XML Parser does not report CDATA events. 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 factory = XMLInptuFactory.newInstance();
factory.setProperty("report-cdata-event", Boolean.TRUE);

The parser we are using does not support the 'report-cdata-event' property.

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.

UPDATE:

After posting this I browsed some of the related questions and there was mention of the 'isCoalescing' property; for the record it is being set to FALSE.

like image 787
user1110046 Avatar asked Dec 21 '11 14:12

user1110046


2 Answers

The correct property is: "http://java.sun.com/xml/stream/properties/report-cdata-event". The property XMLInputFactory.IS_COALESCING must be set to false (default setting).

If these conditions are met, it works fine (tested with Oracle Java 7u51).

like image 162
rmuller Avatar answered Oct 16 '22 23:10

rmuller


Pretty sure Woodstox should handle that.

like image 1
kschneid Avatar answered Oct 16 '22 22:10

kschneid