日期:2014-05-16  浏览次数:20689 次

请问用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>