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
EkanshSrivastava
Posts: 49
Registered: ‎08-13-2012
My Device: 9300
My Carrier: Airtel
Accepted Solution

How to parse XML data coming from server BB10 Cascades Development

Hi All,

 

I am new to BB10 Development.

 

I have made connection with my server which is sending data in XML Format. How to parse it.  Can anyone help me with this.

Also how to store the parsed data in Global Variable or ArrayList. 

 

Regards

Ekansh

Please use plain text.
Developer
EkanshSrivastava
Posts: 49
Registered: ‎08-13-2012
My Device: 9300
My Carrier: Airtel

Re: How to parse XML data coming from server BB10 Cascades Development

Any sample would be helpful. :smileyhappy:
Please use plain text.
New Contributor
manu_dazlia
Posts: 9
Registered: ‎11-08-2012
My Device: BB 10
My Carrier: Divum

Re: How to parse XML data coming from server BB10 Cascades Development

Hi,

 

Refer to the following example. pretty straight forword and flexible json too.

 

https://github.com/blackberry/Cascades-Samples/tree/master/rssnews

Please use plain text.
Developer
EkanshSrivastava
Posts: 49
Registered: ‎08-13-2012
My Device: 9300
My Carrier: Airtel

Re: How to parse XML data coming from server BB10 Cascades Development

Hi,

 

Thanks for the reply. 

 

The link you gave is for parsing JSON data.

 

Please provide some code sample to parse XML.

 

 

Regards

Ekansh

Please use plain text.
New Contributor
manu_dazlia
Posts: 9
Registered: ‎11-08-2012
My Device: BB 10
My Carrier: Divum

Re: How to parse XML data coming from server BB10 Cascades Development

Hi,

 

Please have a close look into the sample rssnews. First listing parses json,

But if you click on any category say for example top news (its goin to parse xml data).

 

Top News Link   http://news.google.com/news?topic=h&output=rss

Please use plain text.
Developer
EkanshSrivastava
Posts: 49
Registered: ‎08-13-2012
My Device: 9300
My Carrier: Airtel

Re: How to parse XML data coming from server BB10 Cascades Development

HI,

 

Following is the code where i get reponse from server.

 

void KayakoDevSample::requestFinished(QNetworkReply* reply)
{
	/*!
	 * @brief QString object to be used to store server response
	 */
	QString response;



	if (reply)
	{
		if (reply->error() == QNetworkReply::NoError)
		{
			int available = reply->bytesAvailable();
			if (available > 0)
			{
				QByteArray buffer(reply->readAll());
				response = QString(buffer);
			}
			else
				response = "Zero bytes in response";

		}
		else
		{
			int httpStatus = reply->attribute(
					QNetworkRequest::HttpStatusCodeAttribute).toInt();
			response =
					"Error and the code is "
							+ reply->attribute(
									QNetworkRequest::HttpStatusCodeAttribute).toString()
							+ "  And the Error string is :: "
							+ reply->errorString() + "\n";
		}
		reply->deleteLater();
	}
	else
	{
		response = "Response comes out to be null";
	}

	if ((response.trimmed().isEmpty()))
	{
		response = "Empty Response";
	}
	qDebug() << "Response String is :: " << response << " \n";

	if (!response.isNull())
	{
		// You got response . Do whatever you want to do with it.
	}

}

 

I recieve XML data in Qstring 'response'.  Now i want parse XML QString. Please help.

 

Thanks 

Ekansh

Please use plain text.
Developer
simon_hain
Posts: 16,113
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to parse XML data coming from server BB10 Cascades Development

I thought you wanted to do it in cascades (qml).
in Qt i use

QByteArray data = currentReply->readAll();
xml.addData(data); //xml is a QXmlStreamReader

 

to parse xml, it is pretty straightforward if you look at the API.

 

Here is a snippet from my code:

while (!xml.atEnd()) {
	xml.readNext();
	/** start of an element **/
	if (xml.isStartElement()) {
		currentTag = xml.name().toString();
		if (xml.name() == "version") {

the stream reader goes hierarchically, so you can simply call sub-routines to parse different parts of the xml if the name() matches a certain value.

 

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
EkanshSrivastava
Posts: 49
Registered: ‎08-13-2012
My Device: 9300
My Carrier: Airtel

Re: How to parse XML data coming from server BB10 Cascades Development

Hi Simon,

Thanks a lot for the code snippet.

One more problem i am facing.
To fetch the value of the tag.

I am using xml.readElementText ().

It returns me blank value.

Plz help.

Thanks
Please use plain text.
Developer
simon_hain
Posts: 16,113
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to parse XML data coming from server BB10 Cascades Development

i am also using readElementText. i would suggest that you use breakpoints, also print the received xml to the console and check it with an editor.
notepad++ is very good for that as it can reformat xml and has syntax highlighting (if you set the language to xml)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
EkanshSrivastava
Posts: 49
Registered: ‎08-13-2012
My Device: 9300
My Carrier: Airtel

Re: How to parse XML data coming from server BB10 Cascades Development

Hi Simon,

 

I tired doing the same. Also did debuging and printed on console.

 

My XML response:

 

<kayako_staffapi>
<status><![CDATA[1]]></status>
<error />
<version><![CDATA[4.51.1891]]></version>
<sessionid><![CDATA[5t8hzmxq14jv9npkoy8e9oh8nobt9p8y]]></sessionid>
<sessiontimeout><![CDATA[21600]]></sessiontimeout>
<staffid><![CDATA[1]]></staffid>
</kayako_staffapi>"

 

 

Still i am not able to fetch the data in between the tags.

 

Regards

Ekansh

Please use plain text.