</pre><pre name="code" class="javascript"><p>
</p><p><DOCTYPE html></p><html>
	<head>
		<title>2_6</title>
		<meta charset="utf-8"/>
	</head>
	<body οnlοad="process()">
		<div id="myDiv">
			
		</div>
	</body>
	<script>
		var xmlHttp=createXmlHttpRequestObject();
		function createXmlHttpRequestObject(){
			var xmlHttp;
			try{
				xmlHttp=new XMLHttpRequest();
			}catch(e){
				var XmlHttpVersions=new Array(
				"MSXML2.XMLHTTP.6.0",
				"MSXML2.XMLHTTP.5.0",
				"MSXML2.XMLHTTP.4.0",
				"MSXML2.XMLHTTP.3.0",
				"MSXML2.XMLHTTP",
				"Mircosoft.XMLHTTP"
				);
				
				for(var i=0;i<XmlHttpVersions.length&&!xmlHttp;i++){
					try{
						xmlHttp=new ActiveXObject(XmlHttpVersion[i]);
					}catch(e){
						}
					}
				}
			if(xmlHttp){
				return xmlHttp;
				}else{
					alert('Error create the XMLHttpRequest object.');
					}
			}
			
		function process(){
			if(xmlHttp){
				try{
					xmlHttp.open("GET","books.xml",true);
					xmlHttp.onreadystatechange=handleRequestStateChange;
					xmlHttp.send(null);
				}catch(e){
					alert("Can't connect to server:\n"+e.toString());
					}
				}
			}
			
			function handleRequestStateChange(){
				if(xmlHttp.readyState==4){
					if(xmlHttp.status==200){
						try{
							handleServerResponse();
						}catch(e){
							alert("Error reading the response:"+e.toString());
							}
						}else{
							alert("There was a problem retrieving the data:\n"+xmlHttp.statusText);
							}
					}
				}
				
			function handleServerResponse(){
				var xmlResponse=xmlHttp.responseXML;
				//XML文件格式一定要正确
				xmlRoot=xmlResponse.documentElement;
				//注意getElementsByTagName
				titleArray=xmlRoot.getElementsByTagName("title");
				isbnArray=xmlRoot.getElementsByTagName("isbn");
				
				var html="";
				for(var i=0;i<titleArray.length;i++){
					html+=titleArray.item(i).firstChild.data+","+isbnArray.item(i).firstChild.data+"<br/>";
					}
				myDiv=document.getElementById('myDiv');
				myDiv.innerHTML="Server says:<br/>"+html;
				}
	</script>
</html>