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 Contributor
Posts: 2
Registered: ‎01-30-2014
My Device: BlackBerry Z10
My Carrier: Vodafone Australia

Display one or certain Xml element

Hey,

 

I am a beginner and still learning. I am trying to make an app that gets weather updates from the Yahoo Weather API, it comes in Xml format.

 

I want to display certain xml elements not everything. For example the temprature and humidity. I have looked through the BlackBerry documentation and it only mentions how to display  lists from xml and not specific elements.

 

Thanks

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Display one or certain Xml element

Welcome to the forums.

 

Can I recommend you look at some of the samples,

 

http://developer.blackberry.com/native/sampleapps/

 

You'll find that the ListView's do actually use the individual elements of XML not the whole thing.

Usually by accessing it drectly as so, ListItemData.temperature.

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.
New Contributor
Posts: 2
Registered: ‎01-30-2014
My Device: BlackBerry Z10
My Carrier: Vodafone Australia

Re: Display one or certain Xml element

[ Edited ]

Thanks for the reply.

 

I have tried to use ListItemData but I think I am missing something.

Heres the code

 

weather.xml

<channel>
<title>Yahoo! Weather - Sunnyvale, CA</title>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA11...
</link>
<description>Yahoo! Weather for Sunnyvale, CA</description>
<language>en-us</language>
<lastBuildDate>Wed, 29 Jan 2014 11:56 pm PST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Sunnyvale" region="CA" country="United States"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="57" direction="310" speed="7"/>
<yweather:atmosphere humidity="67" visibility="10" pressure="30" rising="2"/>
<yweather:astronomy sunrise="7:13 am" sunset="5:30 pm"/>
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>
http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif
</url>
</image>
<item>
<title>Conditions for Sunnyvale, CA at 11:56 pm PST</title>
<geo:lat>37.37</geo:lat>
<geo:long>-122.04</geo:long>
<link>
http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA11...
</link>
<pubDate>Wed, 29 Jan 2014 11:56 pm PST</pubDate>
<yweather:condition text="Cloudy" code="26" temp="57" date="Wed, 29 Jan 2014 11:56 pm PST"/>
<description>
<![CDATA[
<img src="http://l.yimg.com/a/i/us/we/52/26.gif"/><br /> <b>Current Conditions:</b><br /> Cloudy, 57 F<BR /> <BR /><b>Forecast:</b><BR /> Wed - Showers. High: 66 Low: 49<br /> Thu - AM Light Rain. High: 58 Low: 41<br /> Fri - Mostly Sunny. High: 59 Low: 36<br /> Sat - Sunny. High: 59 Low: 35<br /> Sun - Few Showers. High: 59 Low: 36<br /> <br /> <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/USCA11... Forecast at Yahoo! Weather</a><BR/><BR/> (provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]>
</description>
<yweather:forecast day="Wed" date="29 Jan 2014" low="49" high="66" text="Showers" code="11"/>
<yweather:forecast day="Thu" date="30 Jan 2014" low="41" high="58" text="AM Light Rain" code="11"/>
<yweather:forecast day="Fri" date="31 Jan 2014" low="36" high="59" text="Mostly Sunny" code="34"/>
<yweather:forecast day="Sat" date="1 Feb 2014" low="35" high="59" text="Sunny" code="32"/>
<yweather:forecast day="Sun" date="2 Feb 2014" low="36" high="59" text="Few Showers" code="11"/>
<guid isPermaLink="false">USCA1116_2014_02_02_7_00_PST</guid>
</item>
</channel>
</rss>
<!--
 fan484.sports.gq1.yahoo.com Thu Jan 30 08:56:41 PST 2014 
-->

 

main.qml

import bb.cascades 1.0
import bb.data 1.0

Page {
	Container {
        ListView {
            dataModel: XmlDataModel {
                source: "asset:///weather.xml"
            }
            
            listItemComponents: [
                ListItemComponent {
                    Label {
                        text: ListItemData.description
                    }
                }
            ]
  }
      		
  }
}

 

Developer
Posts: 6,152
Registered: ‎07-05-2012
My Device: Playbook, Dev Alpha C, Z10 LE, Z30
My Carrier: Orange

Re: Display one or certain Xml element

Can you read this...

 

http://developer.blackberry.com/native/documentation/cascades/device_platform/data_access/working_wi...

 

ListView's work on attributes of an XML tag you are referencing the tag itself.

Referencing a tag is done by using the itemType() call and so you would be looking for that tag before referencing the attribute.

 

The problem you have is that the XML is not formatted in a way that ListView expects, any strings outside of the tags is ignored.  You will need to preprocess the data in to a sanitised format that ListView can understand first.

 


If you've been helped click on Like Button, if you've been saved buy the app. Smiley Happy

Developer of stokLocker, Sympatico and Super Sentences.