javascript调用XML
本帖最后由 qq83705563 于 2011-10-08 17:38:14 编辑
<!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>
<title>my First ajax</title>
<script type="text/javascript">
var xmlHttp;
function startRequest(){
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
xmlHttp.onreadystatechange=stateChange;
xmlHttp.open("GET","text.xml",true);
xmlHttp.send(null);
}
function stateChange(){
if(xmlHttp.readyState != 4)
{
return;
}
if(xmlHttp.status != 200)
{
alert("Problem retrieving XML data");
return;
}
x=xmlHttp.responseXML.getElementsByTagName("TITLE");
alert(x.length); //=3
alert(x[0].nodeValue); //为什么这个值为null
}
</script>
</head>
<body>
<input type="button" value="test" onclick="startRequest();">
</body>
</html>
<?xml version="1.0" ?>
<!-- Copyright w3school.com.cn
-->
<text>
<cd>
<TITLE>111111</TITLE>
</cd>
<cd>
<TITLE>222222</TITLE>
</cd>
<cd>
<TITLE>333333</TITLE>
</cd>
</text>