Skip to main content

CDATA event never returned

1 reply [Last post]
minka99
Offline
Joined: 2008-03-27
Points: 0

Hello.
This test shows an xml file which contains a CDATA section. The XMLStreamReader
input factory has IS_COALESCING false. Under that condition, I thought the reader
was supposed to return a CDATA event for a CDATA section. But it returns a CHARACTERS
event. Is this a bug or a misunderstanding? Thanks, minka99

// bug? CDATA state never returned.
String xmlStr = "<?xml version=\"1.0\"?> test with a CDATA section ]]> def ";
ByteArrayInputStream xml = new ByteArrayInputStream(xmlStr.getBytes());

XMLInputFactory inputFactory = XMLInputFactory.newInstance();
inputFactory.setProperty(XMLInputFactory.IS_COALESCING, false);
inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);

XMLStreamReader sr = inputFactory.createXMLStreamReader(xml);
int st;
while (sr.hasNext()) {
st = sr.next();
if (st == XMLStreamConstants.CDATA)
System.out.println("Encountered CDATA section");
switch (st) {
case XMLStreamConstants.CHARACTERS:
System.out.println(sr.getText());
break;
default:
break;
}
}

The output is:

test with a CDATA section
<456>
def

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
joehw
Offline
Joined: 2004-12-15
Points: 0

It's not a bug. The spec. states that All text events get reported as Characters events. Content, CData and whitespace are all reported as Characters events.

Joe