+1
Beantwoord

Simple RSS parsing help needed

kevinbridges 12 jaar geleden bijgewerkt door icahill (Administrator) 12 jaar geleden 8
Hello, newbie here looking for the best way to access one element within a simple RSS feed. I am writing a weather app as practice, and I'd like to use one of the popular weather status RSS feeds to determine current conditions in a zip code. I've looked at the Yahoo weather RSS feed, and their "code" element is exactly what I need. I've gone through the tutorial on using datasources, and it makes sense, Im just having trouble simplifying it down to my need. I've created a timed loop in my app that wants to check this "code" element, and then display an icon based on the results. Any advice on the simplest way to access that one piece of info? Here is a link to the RSS feed: http://weather.yahooapis.com/forecast...

Thanks in advance.
Hey Kevin,

You are right, the attribute inside of a node can be a little tricky to path to with XPath. Here is a simple datasource wire that is using a list and an object tag to parse down to the 'code' element in your feed. I hope this helps.



<wire>
<datasources>
<datasource name="RSSFeed" source="" query="//item" providertype="xml" parsenamespaces="yes" />
</datasources>
<classes>
<class name="article-list-item">
<panel name="article-[param:dataSourceIndex]" width="100%" fitheight="yes" _dataSourceIndex="[param:dataSourceIndex]" background="303339" borderwidth="1" bordercolor="0000FF" padding="2">
<panel name="info2" width="100%" fitheight="yes" padding="2" borderwidth="1" bordercolor="00ff00" >
<text name="headline" width="100%" height="fit" color="#707B51" font="Helvetica-Bold" size="18" align="left" alignment="left" text="[param:headline]" background="303339" fit="yes"></text>
<text name="description" width="100%" height="fit" color="#f0ffff" font="Helvetica" size="14" align="left" bottomof="headline" alignment="left" text="[param:description]" background="303339"></text>
</panel>
</panel>
</class>
</classes>
<main>
<panel name="BasePanel" width="100%" height="100%" borderwidth="1" bordercolor="ffffff" padding="2">
<panel name="Rest" width="100%" height="100%" background="ff0000" borderwidth="1" bordercolor="ff0000" valign="bottom">
<list name="list" datasource="RSSFeed" width="100%" lwidth="100%" height="100%" valign="bottom" align="center" orientation="vertical" paginate="no">
<object class="article-list-item" dataSourceIndex="[param:dataSourceIndex]" headline="[param:title]" description="[param:*[local-name()='forecast']/@code]" />
<panel name="hi" width="100%" height="1"></panel>
</list>
</panel>
</panel>
</main>
<actions>
<action name="boot" oninit="yes">
<assign property="datasource:RSSFeed.source" value="http://weather.yahooapis.com/forecastrss?w=2502265" />
</action>
</actions>
</wire>
This is the key part:


<object class="article-list-item" dataSourceIndex="[param:dataSourceIndex]" headline="[param:title]" description="[param:*[local-name()='forecast']/@code]" />
Perfect! Thanks! I was stuck in a syntax free fall :-)
Hey Ian, thanks for helping me with this. I've tinkered with this for a couple of hours, and I can get some of the functionality I'm looking for, but I seem to keep getting close, but not quite there. I am trying to create an app that plays a particular video based on the current time of day and weather conditions (using Yahoo RSS feed) in a particular zip code. My library of videos are name coded so that they correspond to these scenarios. This is supposed to be like looking out a virtual window. My basic design is to have the app check current conditions (behind the scenes) every x minutes, and adjust the full screen video that is playing, if needed. My questions are:

-What's your recommendation for creating the time delayed function that checks for a new weather code every x minutes? I tried a tag with a delay, but I'm getting odd results.

- Seems like I have to use a class/object to get the RSS weather code into the app, how do I pass that code back down to an action where I can use it to feed the video controller source?

- Finally, how would you approach this project (in other words, am I going about it all wrong)?

Thanks for your help. I love this application, and I want to start using it more frequently, so I'm trying to get comfortable with the developer tools.
1) A delay would be the best bet for checking your current conditions. What do you mean when you say odd?

The code could look something like this:

<action name="check-weather" oninit="yes" onresume="yes">
<sync loop="yes" delay="60">
<play action="weatherdata" />
</sync>
</action>
<action name="weatherdata">
<if lhs="[datasource:weather.1.condition]" operator="e" rhs="23">
PLAY SUNNY VIDEO
</if>
IF...etc.
</action>


2) When you are using a class, you can treat it just like you would any other object in the main block, meaning that you can place onclicks, etc. onto the class and those can fire actions. You can also add object variables (_something) that actions fired directly on the object can read.


EX.
<class name="amigos">
<panel name="[param:name]" width="15%" height="60%" xpos="[param:x]" ypos="33%" onclickup="set-variable" _amigo="[param:amigo]"></panel>
</class>
.....
<action name="set-variable">
<assign property="var:amigo" value="_amigo" />
<alpha value="1" time="0" target="PANEL" />
<assign property="object:TEXT.text" value="Starring [var:amigo]!" />
</action>



3) Listening to you talk about the project sounds like you have a lot figured out already. We are glad you like it so far. If you want to send me a collaborator invite I would love to take a look at the code and see how it is going!

Thanks.
Thanks again for your help Ian. I made big strides today. I think I've got the basics in place to where it's more of content exercise now. I will add you as a collaborator so you can check things out. I've got some more questions for you regarding technique, but I have several things to clean up first.
Hello again Ian. I added you as a contributor on my project, but wasn't sure I had your correct email address. I used iancahill@rarewire.com. I soved a lot of my own issues with this test app over the weekend, but I do have a question regarding video. I will start a new topic, as I think you've addressed everything I asked about in this thread. Thanks.
good to hear, I did not get the invite, my email is icahill@rarewire.com :)

Looking forward to seeing it.