解析XML命名空间?

7 javascript xml ajax namespaces

使用JavaScript/Ajax?

我正试图从中提取值:

<yweather:astronomy sunrise="6:34 am"   sunset="8:38 pm"/>
Run Code Online (Sandbox Code Playgroud)

寻找类似的东西:

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

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

Ste*_*son 8

有一个特殊版本的getElementsByTagName命名空间:getElementsByTagNameNS.

例如:

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

... 命名空间[Namespace URI]的URI 在哪里yweather.

史蒂夫

  • IE不支持,但不正确 (2认同)