IE和FF中读取XML的问题 - Web 开发 / Ajax
源代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Parsing XML Responses with the W3C DOM</title>
<script type="text/javascript">
var xmlHttp;
var requestType = "";
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHttp");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
}
function startRequest(requestedList)
{
requestType=requestedList;
createXMLHttpRequest();
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("GET","parseXML.xml",true);
xmlHttp.send(null);
}
function handleStateChange()
{
if(xmlHttp.readyState==4)
if(xmlHttp.status==0)
{
if(requestType=="north")
listNorthStates();
else if(requestType=="all")
listAllStates();
}
}
function listNorthStates()
{
var xmlDoc=xmlHttp.responseXML;
alert(xmlDoc.getElementsByTagName("states").length); [b]//IE中返回0,FF中返回1[/b] alert(xmlDoc.getElementsByTagName("state").length);
//IE中返回0,FF中返回12 var northNode=xmlDoc.getElementsByTagName("north")[0];
//northNode赋值后仍为null var out="Northern States";
var northStates=northNode.getElementsByTagName("state");
outputList(out,northStates);
}
function listAllStates()
{
var xmlDoc=xmlHttp.responseXML;
var out="All States";
var allStates=xmlDoc.getElementsByTagName("state");
outputList(out,allStates)
}
function outputList(tit,states)
{
var out=tit;
for(var i=0;i<states.length;i++)
{
var stateName=states[i].childNodes[0].nodeValue;
tit+="\n-"+stateName;
}
alert(tit);
}
</script>
</head>
<body>
<h1>Process XML Document of U.S. States</h1>
<br/><br/>
<form action="#">
<input type="button" value="View All Listed States" onclick="startRequest('all')" />
<input type="button" value="View All Listed Northern States" onclick="startRequest('north')" />
</form>
</body>
</html>
parseXML.xml
<?xml version="1.0" encoding="UTF-8"?>
<states>
<north>
<state>Minnesota</state>
<