Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 9
Registered: ‎02-15-2011
My Device: Not Specified

XML and CDATA parsing

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?

New Contributor
Posts: 9
Registered: ‎02-15-2011
My Device: Not Specified

Re: XML and CDATA parsing

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

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: XML and CDATA parsing

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?

New Contributor
Posts: 9
Registered: ‎02-15-2011
My Device: Not Specified

Re: XML and CDATA parsing

[ Edited ]

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&nbsp;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?

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: XML and CDATA parsing

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. 

Developer
Posts: 105
Registered: ‎04-24-2012
My Device: blackberry developer
My Carrier: relience

Re: XML and CDATA parsing

Hello,
I am facing same issue.
I am not able to read CData in XMLParser.
Did you solved?
If yes then put the example code here for help.
Regular Contributor
Posts: 90
Registered: ‎09-04-2009
My Device: Blackberry Z10
My Carrier: Airtel

Re: XML and CDATA parsing

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);
}

}

Developer
Posts: 105
Registered: ‎04-24-2012
My Device: blackberry developer
My Carrier: relience

Re: XML and CDATA parsing

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.