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
Developer
klz
Posts: 132
Registered: ‎08-24-2011
My Device: Z10 LE + Playbook

Get a value when attribute exists - XML

Hello everyone,

 

So my problem is that I have no idea how to get a value when an attribute exists. I will try to be more detailed below.

 

I have a XML document with the following sctructure:

 

<listOfStations>
<station>
<StationIcon>0332.jpg</StationIcon>
<StationName>JD Inside</StationName>
<StationURL>http://www.jdinside.com/</StationURL>
<StationStreamURL type="pls">
http://www.panel-streaming.com/tunein.php/jdinside/playlist.pls
</StationStreamURL>
<StationButton/>
<StationButtonSelected/>
</station>
</listOfStations>

 I am receiving correct data from it and pass it ListView as a model. There is no mistake in this part of code. Here is a QVariant:

 

QVariant(QVariantMap, QMap(("StationButton", QVariant(QString, "") ) ( "StationButtonSelected" ,  QVariant(QString, "") ) ( "StationIcon" ,  QVariant(QString, "0332.jpg") ) ( "StationName" ,  QVariant(QString, "JD Inside") ) ( "StationStreamURL" ,  QVariant(QVariantMap, QMap((".data", QVariant(QString, "http://www.panel-streaming.com/tunein.php/jdinside/playlist.pls") ) ( "type" ,  QVariant(QString, "pls") ) )  ) ) ( "StationURL" ,  QVariant(QString, "http://www.jdinside.com/") ) )  ) 

I can get Station name in QML to set is as title for cell - ListItemData.StationName

Can get StationUrl - ListItemData.StationURL

Can get type of Station Stream Url (which is attribute) - ListItemData.StationStreamURL.type


BUT, I have no idea how to get Url (which is value). ListItemData.StationStreamURL.data doesn't work and returns 

 

TypeError: Result of expression 'chosenItem.StationStreamURL' [undefined] is not an object.

 

You would ask me what choseItem is. It is

 var chosenItem = dataModel.data(indexPath);

 I use it in onTrigerred to get an element which has been pressed. 

 

 
Any help will be appreciated. 
 
Thanks! 
 
Thank you!
----------------------------
My Apps: Alchemy Game | Image Resizer | Jam! | Traffic Signs: USA | Radio Record
My Blog: http://bb10pro.com
Developer
Developer
lew
Posts: 174
Registered: ‎03-05-2009
My Device: Dev Alpha

Re: Get a value when attribute exists - XML

i think the element 'StationStreamURL' doesnt always exist?

 

try wrapping into defensive code:

 

if(ListItemData.StationStreamURL){

  ListItemData.StationStreamURL[".data"]

}

 

 

BUT, I have no idea how to get Url (which is value). ListItemData.StationStreamURL.data doesn't work and returns 

 your above code doesn't work, the key is NOT "data" but ".data". So you should use the javascript array accessor notation to retrieve that value (see my example above).

 

Contributor
tipul07
Posts: 11
Registered: ‎10-28-2012
My Device: Z10

Re: Get a value when attribute exists - XML

I searched how to obtain value of a tag when there are attributes for that tag using only QML (was parsing xml feeds directly with QML).

 

<root>
   <tag attr1="valattr1" attr2="valattr2">tagvalue</tag>
   <tag attr1="valattr1" attr2="valattr2">tagvalue</tag>
...
</root>

 

then inside ListView > listItemComponents > ListItemComponent > Container > Label:

 

ListItemData.tag[0].attr1, ListItemData.tag[0].attr2 etc for attributes and ListItemData.tag[0][".data"]

ListItemData.tag[1].attr1, ListItemData.tag[1].attr2 etc for attributes and ListItemData.tag[1][".data"]

...

 

Hope this help

Andy