Exception xml.getElementsByTagName is not a function, when trying to parse an xml

有些话、适合烂在心里 提交于 2019-12-24 22:46:18

问题


I'm trying to parse xml from an notes.xml, it show an error in firebug as

    TypeError: xml.getElementsByTagName is not a function

My code part is,

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>

回答1:


Your alert statement is wrong. x has no method getElementsByTagName.

You can get the first city using:

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

The second one is:

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

And states:

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


来源:https://stackoverflow.com/questions/20416958/exception-xml-getelementsbytagname-is-not-a-function-when-trying-to-parse-an-xm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!