【问题标题】:Exception xml.getElementsByTagName is not a function, when trying to parse an xml尝试解析 xml 时,异常 xml.getElementsByTagName 不是函数
【发布时间】:2013-12-06 05:50:54
【问题描述】:

我正在尝试从 notes.xml 解析 xml,它在 firebug 中显示错误为

    TypeError: xml.getElementsByTagName is not a function

我的代码部分是,

notes.xml

    <fr>
    <franchise city="Scottsdale"  state=" AZ" />
    <franchise city="Arcadia" state=" CA" />
    </fr>

javascript

       <script>                      
        if (window.XMLHttpRequest)
        {
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.open("GET","notes.xml",false);
        xmlhttp.send();
        xmlDoc=xmlhttp.responseXML;
        var x=xmlDoc.getElementsByTagName("franchise");
        alert(x.getElementsByTagName("state")[0].childNodes[0].nodeValue);          

         </script>

【问题讨论】:

    标签: javascript html xml xml-parsing


    【解决方案1】:

    您的警告声明是错误的。 x 没有方法 getElementsByTagName。

    您可以使用以下方式获得第一个城市:

    alert(x[0].attributes[0].nodeValue); // shows Scottsdale
    

    第二个是:

    alert(x[1].attributes[0].nodeValue); // shows Arcadia
    

    并声明:

    alert(x[0].attributes[1].nodeValue);  // AZ
    alert(x[1].attributes[1].nodeValue);  // CA
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 2012-11-04
      • 2017-07-13
      • 1970-01-01
      相关资源
      最近更新 更多