03-07-2011 10:52 AM
I'm trying to parse a XML document which contains some HTML data that is wrapped into a CDATA section in the document. Normally the parser should just return the value of the section and won't care about the contents of the tag. This behavior seems the correct way of interpreting the XML document, and this seems to work correctly on the SDK 6.0 simulator.
When I try and run the same code on a 9780 phone that's also runs on the 6.0 version of the OS, the XML parser in the phone seems to be trying to parse the content of the CDATA content, although it shouldn't, resulting in a failure to parse the XML document.
Is there a way to circumvent this? I already replaced the tags, which gave the initial parsing problem, resulting in a error that the document doesn't contain an end tag for 'hr', which is correct because there is no end tag, but a bit weird because there is no begin tag either.
I'm just using the default settings for the XML parsing:
DocumentBuilderFactory factory = DocumentBuilderFactory
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(fix_nbsp(inputstream));
Element rootElement = doc.getDocumentElement();
Does anyone have a suggestion, it seems that the xml parser isn't behaving correctly on the phone?
03-08-2011 09:30 AM
I rewrote the class to only use the SAX parser instead of the DOM parser to see if there is a difference. Still the SAX parser on the 9780 phone tries to parse CDATA contents instead of just giving its contents back . The seems like incorrect behavior of the XML parser?
And should is the simulator behaving differenlty from the actually phone, when they both run the 6.0.0 version of the SDK
03-09-2011 06:30 PM
Not an area of great expertise, but as I understood it, the CDATA should be encoded so that it does not contain characters that the processing could interpret as XML, for example <. If you are sending CDATA data that contains these characters, then the XML parser has no chance. How do you create this XML data?
03-09-2011 06:44 PM - edited 03-09-2011 06:46 PM
Thank you for your reply. The xml that's troubling me at the moment looks like this:
<title><![CDATA[ title information ]]></title>
<![CDATA[ <p>here goes the text containing HTML</p> tags]]>
It's just XML with a piece of CDATA contained in a descrpition tag, which by my understandig should just be ignored by the XML parser, and therefore should not be encoded. The Blackberry Simulator does this just fine, but the xml parser in the phone seems to trie it's best to parse the contents of the CDATA information. In contrary to the PCDATA sections in a document which should be parsed and therefore encoded.
The information is just downloaded from a xml stream. nothing special about that acutally. But when the parse(inputstream) function is called it fails on the HTML contents of the CDATA tags. Maybe I'm missing a setting to influence the behavior of the parser?
03-09-2011 07:48 PM
Yes you are right, I was not thinking straight in my previous comment.
Sorry I don't actually use CDATA in this way in anything I process, so I have no experience. Hopefully someone else will have done so, otherwise I guess it is something I could try myself.
But having looked again, I think it should be processed correctly as is.
05-14-2012 07:34 AM
i have done the parsing xml using SAX
public class sampleParser extends DefaultHandler
StringBuffer _buffer = null;
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException
if(_buffer != null)
_buffer = null;
_buffer = new StringBuffer();
public void endElement(String uri, String localName, String qName) throws SAXException
if(localName == null)
localName = qName;
_title = _buffer.toString();
public void characters(char ch, int start, int length) throws SAXException
String value = new String(ch,start,length);
if(_buffer != null)
super.characters(ch, start, length);
05-14-2012 08:03 AM
I also found the solution in the XMLParser only.
you need to allow property of Document builder as true,
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
So If your any node contain cdata it will return you in the node.getnodevalue.