【问题标题】:Parsing XML namespaces?解析 XML 命名空间?
【发布时间】:2010-11-08 04:29:49
【问题描述】:

使用 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;

但到目前为止没有任何效果。 :'( 谢谢。

【问题讨论】:

    标签: javascript xml ajax namespaces


    【解决方案1】:

    对于命名空间有一个特殊版本的getElementsByTagNamegetElementsByTagNameNS

    例如:

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

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

    史蒂夫

    【讨论】:

    • @annakata:出于兴趣,你在IE里怎么做?
    • 我认为您可以将 getElementsByTagName("yweather:astronomy") 与 IE 一起使用。此外,Google feeds API 具有跨浏览器实现。也许你可以使用它,或者类似的东西:code.google.com/apis/ajaxfeeds/documentation/…
    • 在 MSXML 中,您可以在 XML DOMDocument 对象上设置“SelectionNamespaces”属性,然后将非标准“selectNodes”(或“selectSingleNode”)方法与 XPath 选择器一起使用。有关示例,请参见 xml.com/pub/a/2002/06/05/msxml4.html
    • @NickFitz:啊,我明白了。谢谢!
    • 您可能需要转义冒号“yweather\\:astronomy”
    猜你喜欢
    • 2021-05-03
    • 2014-01-10
    • 1970-01-01
    • 2012-08-28
    • 2018-07-15
    • 2012-06-11
    • 2018-12-07
    相关资源
    最近更新 更多