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
New Developer
Ooata
Posts: 10
Registered: ‎01-14-2013
My Device: Blackberry 10 Simulator
My Carrier: EE
Accepted Solution

XML File not working with XmlDataModel

[ Edited ]

Hi all,

 

I am trying to develop a simple image reader using the online service www.imgur.com. It's going to be free and ad-free which makes using the normal API easy enough. I have followed tutorials from the dev site to send a http request and to receive the reply. I have managed to do all that find but I am stumped with trying to apply the http reply to my ListView.

 

The reply is an XML file but with child-elements rather than attributes for information. I have been trying to get my reply working with the tutorial here:

 

Blackberry Dev Networking Tutorial

 

specifically this part of code

 

// Create the data model using the contents of the file. The
        // location of the file is relative to the assets directory.
        XmlDataModel *dataModel = new XmlDataModel();
        dataModel->setSource(QUrl("file://" + QDir::homePath() + "/model.xml"));
 
        // Set the new data model on the list and stop the activity indicator
        mListView->setDataModel(dataModel);

 

But I just can't get it to work. I have read from various people (but not confirmed on anything doc) that the XmlDataModel can't be used with child-element type XML files? If so, can anyone point me in the direction of converting to an attribute type XML file? 

Please use plain text.
Developer
Zmey
Posts: 1,510
Registered: ‎12-18-2012
My Device: PlayBook, Z10, DAC

Re: XML File not working with XmlDataModel

Hi! You can use QXmlStreamReader to parse the XML and push data into GroupDataModel or ArrayDataModel.

Andrey Fidrya, @zmeyc on twitter
Please use plain text.
Developer
taylortbb
Posts: 137
Registered: ‎10-26-2010
My Device: Bold 9900 + PlayBook
My Carrier: Rogers

Re: XML File not working with XmlDataModel

The DataSource object can handle child element XML lists, and it handles the network request part too. It is however quite limited in its ability to handle complex multi-level structures, so it may or may not be appropriate for your use. Without an example XML file I can't say for sure whether DataSource would handle it, but it's probably something worth investigating.
--------
Taylor Byrnes
Please use plain text.
New Developer
Ooata
Posts: 10
Registered: ‎01-14-2013
My Device: Blackberry 10 Simulator
My Carrier: EE

Re: XML File not working with XmlDataModel

[ Edited ]

Thanks for the replies! Using a DataSource object looks the easiest way to go about it, the only hesitation I have is that is that to access the api I need to put an identifying key in my request header. Currently it is done using

 

request.setRawHeader("Authorization", "Client-ID MY_CLIENT_ID");

 Is there a way to do this using a DataSource object (or globally?)? The XML return from the server looks quite simple, I've attached a sample of it below to show the general structure:

 

<?xml version="1.0" encoding="utf-8"?>
<data success="1" status="200">
	<item>
		<id>Cot4g</id>
		<title>So someone posted a split pic earlier</title>
		<datetime>1358127556</datetime>
		<type>image/png</type>
		<animated>false</animated>
		<width>460</width>
		<height>611</height>
		<size>382970</size>
		<views>1059391</views>
		<bandwidth>405714971270</bandwidth>
		<vote/>
		<account_url/>
		<link>http://i.imgur.com/Cot4g.png</link>
		<ups>2479</ups>
		<downs>35</downs>
		<score>3305</score>
		<is_album>false</is_album>
	</item>
	<item>
		<id>Xz56B</id>
		<title>Shower this man n Karma</title>
		<datetime>1358133626</datetime>
		<type>image/jpeg</type>
		<animated>false</animated>
		<width>800</width>
		<height>595</height>
		<size>84355</size>
		<views>675548</views>
		<bandwidth>56985851540</bandwidth>
		<vote/>
		<account_url/>
		<link>http://i.imgur.com/Xz56B.jpg</link>
		<ups>2795</ups>
		<downs>111</downs>
		<score>3385</score>
		<is_album>false</is_album>
</item>
</data>

 I've edited it to show the hierarchy of response. As you can see there is a root <data> element and then <item> elements which contain the various things I want to use. Is this simple enough for DataSource object to work?

 

Thanks for all your help!

 

 

Please use plain text.
New Developer
Ooata
Posts: 10
Registered: ‎01-14-2013
My Device: Blackberry 10 Simulator
My Carrier: EE

Re: XML File not working with XmlDataModel

Thanks taylorbb - I managed to get something working and solved my problems above. I now have a different question but I'll start a new thread for that.

Please use plain text.