Search code examples
javascriptxmlajaxyahooweather

Finding non-closed XML nodes (Yahoo weather)


I'm trying to use Javascript to pull data from Yahoo Weather (eg http://weather.yahooapis.com/forecastrss?w=9807&u=c).

I've looked at other threads including Trying to pull in elements from Yahoo Weather XML and Trying to retrieve yahoo weather using jquery / php / xml and Parsing XML namespaces?

I've set up a proxy on my server so Ajax can grab the file, and it opens up fine (eg I can write the documentElement). However, I'm having problems grabbing the tags such as

<yweather:condition text="Mostly Cloudy" code="28" temp="5" date="Mon, 06 Feb 2012 10:00 am PST"/>

I think it's because it's not a close tag like

<ttl>60</ttl>

Can anyone help me with this? Using Javascript Thanks

var parser = new DOMParser();
xmlDoc = parser.parseFromString(xmlDoc, "text/xml");

alert(xmlDoc.getElementsByTagName("ttl")[0].childNodes[0].nodeValue); //works
alert(xmlDoc.getElementsByTagName("yweather:location")[0].childNodes[0].getAttribute("city")); //doesn't

Solution

  • Read that 'Parsing XML Namespaces' answer again.

    The element you're looking for has a namespace, so you need to use getElementsByTagNameNS.