查找非封闭的XML节点(Yahoo天气)

Finding non-closed XML nodes (Yahoo weather)

本文关键字:Yahoo 天气 节点 XML 查找      更新时间:2023-09-26

我正在尝试使用Javascript从Yahoo Weather中提取数据(例如http://weather.yahooapis.com/forecastrss?w=9807&u=c)。

我看了其他线程,包括尝试从Yahoo Weather XML中提取元素和尝试使用jquery/php/XML检索Yahoo Weather以及解析XML命名空间?

我在服务器上设置了一个代理,这样Ajax就可以获取文件,并且它打开得很好(例如,我可以编写documentElement)。然而,我在获取等标签时遇到了问题

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

我认为这是因为它不像那样是一个封闭的标签

<ttl>60</ttl>

有人能帮我吗?使用Javascript感谢

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

再次阅读"解析XML命名空间"的答案。

您要查找的元素有一个名称空间,因此需要使用getElementsByTagNameNS