Scala and XPath - get the first element of an array

Scala/XPath FAQ: How do I get the first element of an array in an XML document using Scala and XPath?

I ran into the problem of needing to get the first array element from an XML document using Scala and XPath recently, and in short, I ended up writing some Scala/XPath code that looked like this:

val day = (xml \\ "channel" \\ "item" \ "forecast")(0) \ "@day"
val date = (xml \\ "channel" \\ "item" \ "forecast")(0) \ "@date"
val low = (xml \\ "channel" \\ "item" \ "forecast")(0) \ "@low"
val high = (xml \\ "channel" \\ "item" \ "forecast")(0) \ "@high"
val text = (xml \\ "channel" \\ "item" \ "forecast")(0) \ "@text"

As you can probably see from that code, I'm getting weather forecast information from an XML document, specifically the Yahoo weather forecast data. The "(0)" at the end of the XPath statement is where I grab the first array element.

To give you a little more information about the problem, the weather forecast data is buried inside a large XML document, and the lines related to the forecast look like this:

<yweather:forecast day="Thu" date="10 Nov 2011" low="37" high="58" text="Partly Cloudy" code="29" />
<yweather:forecast day="Fri" date="11 Nov 2011" low="39" high="58" text="Mostly Cloudy" code="28" />

Putting the Scala XPath code together with the XML, I hope you can see what I'm doing there.

The full Yahoo weather XML document

If you want to understand more about that XPath statement, here's what the full Yahoo weather XML document looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>    
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>Yahoo! Weather - Broomfield, CO</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Broomfield__CO/*http://weather.yahoo.com/forecast/USCO0044_f.html</link>
<description>Yahoo! Weather for Broomfield, CO</description>
<language>en-us</language>
<lastBuildDate>Thu, 10 Nov 2011 2:54 pm MST</lastBuildDate>
<ttl>60</ttl><yweather:location city="Broomfield" region="CO"   country="US"/>
<yweather:units temperature="F" distance="mi" pressure="in" speed="mph"/>
<yweather:wind chill="61"   direction="300"   speed="5" />
<yweather:atmosphere humidity="3"  visibility=""  pressure="30.11"  rising="2" />
<yweather:astronomy sunrise="6:39 am"   sunset="4:47 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 Broomfield, CO at 2:54 pm MST</title>
<geo:lat>39.92</geo:lat>
<geo:long>-105.09</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Broomfield__CO/*http://weather.yahoo.com/forecast/USCO0044_f.html</link>
<pubDate>Thu, 10 Nov 2011 2:54 pm MST</pubDate>
<yweather:condition  text="Partly Cloudy"  code="30"  temp="61"  date="Thu, 10 Nov 2011 2:54 pm MST" />

<description>
<![CDATA[<img src="http://l.yimg.com/a/i/us/we/52/30.gif"/><br />
<b>Current Conditions:</b><br />Partly Cloudy, 61 F<BR />
<BR /><b>Forecast:</b>
<BR />Thu - Partly Cloudy. High: 58 Low: 37
<br />Fri - Mostly Cloudy. High: 58 Low: 39<br />
<br /><a href="http://us.rd.yahoo.com/dailynews/rss/weather/Broomfield__CO/*http://weather.yahoo.com/forecast/USCO0044_f.html">Full Forecast at Yahoo! Weather</a><BR/>
<BR/>(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>]]></description>

<yweather:forecast day="Thu" date="10 Nov 2011" low="37" high="58" text="Partly Cloudy" code="29" />
<yweather:forecast day="Fri" date="11 Nov 2011" low="39" high="58" text="Mostly Cloudy" code="28" />
<guid isPermaLink="false">USCO0044_2011_11_11_7_00_MST</guid>
</item>
</channel>
</rss>
<!-- api1.weather.sp2.yahoo.com uncompressed/chunked Thu Nov 10 14:23:09 PST 2011 -->

If you put my XPath code together with this XML, I think you'll understand the full XPath statement, including the channel, item, and forecast elements.

I hope this Scala XPath XML "array" example has been helpful.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.