用Ajax读取XML格式的数据只需要读取XMLHttpRequest对象返回的responseXML属性即可代码如下
以下是引用片段
<!DOCTYPEhtmlPUBLIC//WC//DTDXHTMLStrict//EN
strictdtd>
<html>
<head>
<title>AjaxHelloWorld</title>
<scripttype=text/javascript>
varxmlHttp;
functioncreateXMLHttpRequest(){
if(windowActiveXObject){
xmlHttp=newActiveXObject(MicrosoftXMLHTTP);
}
elseif(windowXMLHttpRequest){
xmlHttp=newXMLHttpRequest();
}
}
functionstartRequest(){
createXMLHttpRequest();
try{
xmlHttponreadystatechange=handleStateChange;
xmlHttpopen(GETdataxmltrue);
xmlHttpsend(null);
}catch(exception){
alert(您要访问的资源不存在!);
}
}
functionhandleStateChange(){
if(xmlHttpreadyState==){
if(xmlHttpstatus==||xmlHttpstatus==){
//取得XML的DOM对象
varxmlDOM=xmlHttpresponseXML;
//取得XML文档的根
varroot=xmlDOMdocumentElement;
try
{
//取得<info>结果
varinfo=rootgetElementsByTagName(info);
//显示返回结果
alert(responseXMLsvalue:+info[]firstChilddata);
}catch(exception)
{
}
}
}
}
</script>
</head>
<body>
<div>
<inputtype=buttonvalue=returnajaxresponseXMLsvalue
onclick=startRequest();/>
</div>
</body>
</html>
Server dataxml
><?xmlversion=encoding=GB?>
<root>
<info>helloworld!</info>
</root>