当试图解析xml时,异常xml.getElementsByTagName不是一个函数 [英] Exception xml.getElementsByTagName is not a function, when trying to parse an xml

查看:468
本文介绍了当试图解析xml时,异常xml.getElementsByTagName不是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  TypeError:xml我试图从notes.xml解析xml,它在firebug中显示错误.getElementsByTagName不是函数

我的代码部分是,

notes.xml

 < fr> 
<特许经营城市=Scottsdale州=AZ/>
<特许经营城市=Arcadiastate =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>


解决方案

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



第一个城市使用:

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

第二个是:

 警报(X [1] .attributes [0] .nodeValue); //显示Arcadia 

并声明:

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


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>

解决方案

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

这篇关于当试图解析xml时,异常xml.getElementsByTagName不是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆