Exception xml.getElementsByTagName is not a functi

2019-08-29 12:32发布

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条回答
爷的心禁止访问
2楼-- · 2019-08-29 13:29

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
查看更多
登录 后发表回答