ajax 操作xml

liangling221 2008-12-02 06:32:48
<!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文件不起作用请大家看看谢谢
...全文
155 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
草帽 2008-12-22
  • 打赏
  • 举报
回复
address =properties.getElementByTagName("address")[0].firstChild.nodeValue;
price = properties.getElementByTagName("price")[0].firstChild.nodeValue;
comments = properties.getElementByTagName("comments")[0].firstChild.nodeValue;


应该是 properties[i].getElementsByTabName吧

语法错误真多!
草帽 2008-12-22
  • 打赏
  • 举报
回复

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);

properties应该是properties[i]吧

草帽 2008-12-22
  • 打赏
  • 举报
回复
function handleStateChange(){
if(xmlHttp.readyState == 4){
if(xmlHttp.status == 0){
clearPreviousResults();
parseResults();
}
}
}
这段代码中xmlHttp.status == 200才对吧。
yyh84yangtaohui 2008-12-21
  • 打赏
  • 举报
回复
up,up!
haoweishow01 2008-12-18
  • 打赏
  • 举报
回复
先收藏了
ZhangYaoxing 2008-12-18
  • 打赏
  • 举报
回复
同意5楼。不过如果你用FF或者Safari或者Chrome的话,他们是有内建web server的,可以直接用本地访问,IE就不成了。
西安风影 2008-12-17
  • 打赏
  • 举报
回复
代码没仔细看,感觉没啥错误。

不过注意一点的是,关于ajax直接双击html页面是不会有效果的
必须部署到web服务上,然后在浏览器敲地址。
polimo 2008-12-03
  • 打赏
  • 举报
回复
var req;//XMLHTTPRequest对象
var arrOptions = new Array();
if(window.XMLHttpRequest){
req=new XMLHttpRequest();
}else if(window.ActiveXObject){
req=new ActiveXObject("Microsoft.XMLHTTP");
}
var xmlDoc=req.responseXML.documentElement;//获得返回的 xml文档
var node=xmlDoc.getElementsByTagName('dtag');//获得所有 <info>标记
arrOptions =new Array();
for(var i=0;i <node.length;i++){
arrOptions[i]=node[i].firstChild.nodeValue;
}
feng_ocean 2008-12-03
  • 打赏
  • 举报
回复
路过看一下!XML我不是很熟!!
bluefcxt 2008-12-03
  • 打赏
  • 举报
回复
dynamicContent.xml没放错地吧
dayizhixiaotutu 2008-12-02
  • 打赏
  • 举报
回复
这好像是某本书上的代码 可以访问到的 lz仔细看看

52,782

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧