首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Ajax >

请教用ajax怎么得到xml节点值

2013-09-05 
请问用ajax如何得到xml节点值请问下面的代码,如何得到books.xml里的值,谢谢!DOCTYPE html PUBLIC -//W3C

请问用ajax如何得到xml节点值
请问下面的代码,如何得到books.xml里的值,谢谢


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>
function ajax(url, fnSucc, fnFaild)
{
//1.创建ajax对象
var oAjax=null;
if(window.XMLHttpRequest)
{
oAjax=new XMLHttpRequest();
}
else
{
oAjax=new ActiveXObject("Microsoft.XMLHTTP");
}
oAjax.open('GET', url, true);
oAjax.send();
oAjax.onreadystatechange=function ()
{
if(oAjax.readyState==4)
{
if(oAjax.status==200)
{
fnSucc(oAjax.responseText);

}
else
{
if(fnFaild)
{
fnFaild();
}
}
}
};
}
</script>

<script>
window.onload=function ()
{
ajax('books.xml',function (str)
  {
alert("如何得到xml值如Lee或hhh")

  }
  )
}
</script>
</head>
<body>
</body>
</html>


books.xml

<root>
<user>Lee</user>
<email>yc</email>
<url>http://www.baidu.com</url>
</root>
<root>
<user>ddd</user>
<email>hhh</email>
<url>http://www.baidu.com</url>
</root>
XML Ajax
[解决办法]
首先你的xml格式不正确,要增加根节点
<roots><root>
    <user>Lee</user>
    <email>yc</email>
    <url>http://www.baidu.com</url>
</root>
<root>
    <user>ddd</user>
    <email>hhh</email>
    <url>http://www.baidu.com</url>


</root></roots>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script>
    function ajax(url, fnSucc, fnFaild) {
        //1.创建ajax对象
        var oAjax = null;
        if (window.XMLHttpRequest) {
            oAjax = new XMLHttpRequest();
        }
        else {
            oAjax = new ActiveXObject("Microsoft.XMLHTTP");
        }
        oAjax.open('GET', url, true);
        oAjax.send();
        oAjax.onreadystatechange = function () {
            if (oAjax.readyState == 4) {
                if (0 == oAjax.status 
[解决办法]
 oAjax.status == 200) {
                    //fnSucc(oAjax.responseText);
                    fnSucc(oAjax.responseXML);//////////////////////

                }
                else {
                    if (fnFaild) {
                        fnFaild();
                    }
                }
            }


        };
    }
</script>
 
<script>
    window.onload = function () {
        ajax('books.xml', function (dom) {//////////////////////
            var user = dom.getElementsByTagName('user');
            for(var i=0;i<user.length;i++)alert(user[i].firstChild.nodeValue)

        }, function () { alert('fail') }
          )
    }
</script>
</head>
<body>
</body>
</html>

热点排行