1

I am unable to check for CDATA in XML and read it using XMLEventReader.

The following is the sample:

 <name>HEADERS</name> <data> <![CDATA[ Sat Nov 19 18:50:15 2016 (1672822) ]]> <![CDATA[Sat, 19 Nov 2016 18:50:14 -0800 (PST) ]]> </data> 

The XMLEventReader of Stax api which i am using is as follows:

 while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.isCharacters()) { System.out.println(event.asCharacters().isCData()); System.out.println(event.asCharacters().getData()); } } 

So,when I read the data tag for characters, I get false for event.asCharacters().isCData(). Would like to know how to check for CDATA in EventReader and get the CDATA as well.

0

2 Answers 2

2

Use the following pattern:

 switch (EventType) { case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.CDATA: System.out.println(r.getText()); break; default: break; } 

Complete sample:

import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamReader; public void readCDATAFromXMLUsingStax() { String yourSampleFile = "/path/toYour/sample/file.xml"; XMLStreamReader r = null; try (InputStream in = new BufferedInputStream(new FileInputStream(yourSampleFile));) { XMLInputFactory factory = XMLInputFactory.newInstance(); r = factory.createXMLStreamReader(in); while (r.hasNext()) { switch (r.getEventType()) { case XMLStreamConstants.CHARACTERS: case XMLStreamConstants.CDATA: System.out.println(r.getText()); break; default: break; } r.next(); } } catch (Exception e) { throw new RuntimeException(e); } finally { if (r != null) { try { r.close(); } catch (Exception e) { throw new RuntimeException(e); } } } } 

With /path/toYour/sample/file.xml

 <data> <![CDATA[ Sat Nov 19 18:50:15 2016 (1672822)]]> <![CDATA[Sat, 19 Nov 2016 18:50:14 -0800 (PST)]]> </data> 

Gives:

 Sat Nov 19 18:50:15 2016 (1672822) Sat, 19 Nov 2016 18:50:14 -0800 (PST) 
Sign up to request clarification or add additional context in comments.

Comments

1

Set the property, otherwise reader ignores the event type of CDATA.

XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty(Constants.ZEPHYR_PROPERTY_PREFIX + Constants.STAX_REPORT_CDATA_EVENT,Boolean.TRUE); 

See com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl, line: 3027

2 Comments

This should actually be the accepted answer.
For those who don't want to dig through the code, the name of the property is http://java.sun.com/xml/stream/properties/report-cdata-event for the JDK-default InputFactory implementation i.e. com.sun.xml.internal.stream.XMLInputFactoryImpl. (refer to stackoverflow.com/a/22384604)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.