解析XML命名空间?

时间:2009-07-05 06:27:50

标签: javascript xml ajax namespaces

使用JavaScript / Ajax?

我正在尝试从以下位置提取值:

<yweather:astronomy sunrise="6:34 am"   sunset="8:38 pm"/>

寻找类似的东西:

var response = transport.responseXML.getElementsByTagName("channel");
sunrise = response[0].getElementsByTagName("yweather:astronomy").item(0).Attributes["sunrise"].Value;

但到目前为止没有任何作用。 :'( 感谢。

1 个答案:

答案 0 :(得分:8)

名称空间有getElementsByTagName的特殊版本:getElementsByTagNameNS

例如:

var response = transport.responseXML.getElementsByTagName("channel");
var sunrise = response[0].getElementsByTagNameNS("[Namespace URI]", "astronomy")[0].getAttribute("sunrise");

...其中[Namespace URI]yweather命名空间的URI。

史蒂夫

相关问题