服务端代码
<%
response.setContentType("text/xml"); //解析为XML档
response.setHeader("Cache-Control","no-cache");
//out.println("<?xml version='1.0' encoding='ISO-8859-1'?>"); 不能插入该条代码
out.println("<company>");
out.println("<date>");
out.println("<compname>compname</compname>");
out.println("<contname>contname</contname>");
out.println("<address>address</address>");
out.println("<city>city</city>");
out.println("<country>country</country>");
out.println("</date>");
out.println("<date>");
out.println("<compname>compname2</compname>");
out.println("<contname>contname2</contname>");
out.println("<address>address2</address>");
out.println("<city>city2</city>");
out.println("<country>country2</country>");
out.println("</date>");
out.println("</company>");
%>
客户端读取服务端返回的XML数据:
var msg=XMLHttpReq.responseXML; var catalogId=msg.getElementsByTagName("city")[0].firstChild.nodeValue; //获取第一个data内的city数据 alert(catalogId); alert(msg.getElementsByTagName("country")[1].childNodes[0].nodeValue);//获取第二个data内的country数据