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

Native Development

Reply
Developer
Posts: 34
Registered: ‎05-11-2012
My Device: Bold 9900
My Carrier: Vodacom
Accepted Solution

Parsing xml with QT

Hello

 

Is there a way to parse XML in a QString? All the examples on the web shows how to parse the xml from file but none of them shows how to parse XML from a QString. I have a server that my app connects to via TCP/IP and the server then sends XML to the client (app) that needs to be parsed, it will be a waste of resources to first write the xml to a file.

 

I have the basic sax parser and my implementation looks like this

 

XMLHandler *m_handler;

QXmlInputSource *source = new QXmlInputSource(new QFile("asset:///responseMessage.xml"));

QXmlSimpleReader reader;
reader.setContentHandler(m_handler);

reader.parse(source);

 

How do i change this line

 

QXmlInputSource *source = new QXmlInputSource(new QFile("asset:///responseMessage.xml"));

 

to use a string as input and not a file? I know the QXmlInputSource class does not support a string implementation so is there another approach in the API that i can use?

 

Thank you for any advice!

Developer
Posts: 188
Registered: ‎01-27-2012
My Device: playbook
My Carrier: ...

Re: Parsing xml with QT

Perhaps QRegExp is what your looking for? Here is a brief example in case it may help: http://stackoverflow.com/questions/3582039/parse-and-remove-part-of-a-qstring

Good luck!

Jon

Developer
Posts: 34
Registered: ‎05-11-2012
My Device: Bold 9900
My Carrier: Vodacom

Re: Parsing xml with QT

Hi Jon

 

Thanks for the reply. I think that could definitely work (although it would be rather tedious), I just wanted to use XML parsing standars to do this (as you can do this in the older java blackberry sdk). I think that I will go with that method if I cannot find a solution that uses the xml standards.

Developer
Posts: 671
Registered: ‎03-21-2012
My Device: BlackBerry PlayBook 16GB
My Carrier: Vip

Re: Parsing xml with QT

Maybe this is too inefficient, but saving the response from server to new QFile("xxxxx.xml") and loading this file into the parser will do the job. I know it's poor choice.
Highlighted
Developer
Posts: 34
Registered: ‎05-11-2012
My Device: Bold 9900
My Carrier: Vodacom

Re: Parsing xml with QT

I found the solution. I changed my class rather to use a QXmlStreamReader. This can accept QByteArray values in its constructor so i just called the QString toAscii method to convert the string recieved from my server to a QByteArray and passed it to the QXmlStreamReader. This implementation didn't require major changes to my handler so i think this will work the best for my situation. Thanks for all the help guys.