ajax 操作xml
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>�ޱ����ĵ�</title>
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest(){
if(window.ActiveXObject){
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}else if(window.XMLHttpRequest){
xmlHttp =new XMLHttpRequest();
}
}
function doSearch(){
createXMLHttpRequest();
xmlHttp.onreadystatechange =handleStateChange;
xmlHttp.open("GET","dynamicContent.xml",true);
xmlHttp.send(null);
}
function handleStateChange(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 0){
clearPreviousResults();
parseResults();
}
}
}
function clearPreviousResults(){
var header =document.getElementById("header");
if(header.hasChildNodes()){
header.removeChild(header.childNodes[0]);
}
var tableBody =document.getElementById("resultsBody");
while(tableBody.childNodes.length >0){
tableBody.removeChild(tableBody.childNodes[0]);
}
}
function parseResults(){
var results =xmlHttp.responseXML;
alert(results);
var property =null;
var address ="";
var price ="";
var comments ="";
var properties =results.getElementsByTagName("property");
for(var i =0;i<properties.length;i++){
address =properties.getElementByTagName("address")[0].firstChild.nodeValue;
price = properties.getElementByTagName("price")[0].firstChild.nodeValue;
comments = properties.getElementByTagName("comments")[0].firstChild.nodeValue;
addTable(address,price,comments);
}
var header =document.createElement("h2");
var headerText =document.createTextNode("Results:");
header.appendChild(headerText);
document.getElementById("header").appendChild(header);
document.getElementById("resultsTable").setAttribute("border","1");
}
function addTableRow(){
var row =document.createElement("tr");
var cell =createCellWithText("address");
row.appendChild(cell);
cell =createCellWithText(price);
row.appendChils(cell);
cell =createCellWithText("comments");
row.appendChild(cell);
document.getElementById("resultsBody").appendChild(row);
}
function createCellWithText(text){
var cell =document.createElement("td");
var textNode =document.createTextNode(text);
cell.appendChild(textNode);
return cell;
}
</script>
</head>
<body>
<form action="#">
Show Listing from
<select>
<option value="2000">$2000</option>
<option value="3000">$3000</option>
<option value="4000">$4000</option>
</select>
to
<select>
<option value="5000">$5000</option>
<option value="7000">$7000</option>
<option value="8000">$8000</option>
</select>
<input type="button" value="Search" onclick="doSearch()"/>
</form>
<span id="header"></span>
<table id="resultsTable" width="75%" border="0">
<tbody id="resultsBody">
</tbody>
</table>
</body>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property>
<address>西安</address>
<price>$4000</price>
<comments>aaaaaaaaaaa</comments>
</property>
<property>
<address>上海</address>
<price>$3000</price>
<comments>dddddddddd</comments>
</property>
<property>
<address>北京</address>
<price>$3000</price>
<comments>ffffff</comments>
</property>
</properties>
不知道怎么点击按钮访问不到xml文件不起作用请大家看看谢谢