03-07-2011 10:52 AM
Hi,
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
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(fix_nbsp(inputstream));
Element rootElement = doc.getDocumentElement();
rootElement.normalize();
processQuestions(rootElement);
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 - last edited on 03-09-2011 06:46 PM
Thank you for your reply. The xml that's troubling me at the moment looks like this:
<article>
<title><![CDATA[ title information ]]></title>
<description>
<![CDATA[ <p>here goes the text containing HTML</p> tags]]>
</description>
</article>
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.
2 weeks ago
2 weeks ago
i have done the parsing xml using SAX
sample code
public class sampleParser extends DefaultHandler
{
StringBuffer _buffer = null;
String _title;
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;
if(localName.equals("title"))
{
_title = _buffer.toString();
}
}
public void characters(char[] ch, int start, int length) throws SAXException
{
String value = new String(ch,start,length);
if(_buffer != null)
{
_buffer.append(value);
}
super.characters(ch, start, length);
}
}
2 weeks ago
Hello ,
I also found the solution in the XMLParser only.
you need to allow property of Document builder as true,
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setCoalescing(true);
So If your any node contain cdata it will return you in the node.getnodevalue.
Thanks,
bskania.