您的位置:首页 > 其它

自动检索XML数据

2010-02-23 16:34 176 查看
1.books.html

<!--

To change this template, choose Tools | Templates

and open the template in the editor.

-->

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

<head>

<title></title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<script>

var xmlHttpReq = false;

function createXMLHttpRequest(){

try{

//Firefox,Opera8.0+,Safari

xmlHttpReq = new XMLHttpRequest();

}catch(e){

//Internet Explorer

try{

//Internet Explorer 6.0

xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");

}catch(e){

try{

//Internet Explorer 5.5

xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");

}catch(e){

alert("/u4f60的浏览器不支持AJAX!");

return false;

}

}

}

}

function send(url,responseMethod){

createXMLHttpRequest();

xmlHttpReq.open("GET",url,true);

xmlHttpReq.onreadystatechange=responseMethod;//指定响应的函数

xmlHttpReq.send(null);//发送请求

}

//------------------------

function search(){

send('/AjaxApplication/books.xml',parse);

}

function parse(){

if(xmlHttpReq.readyState==4){//对象状态

if(xmlHttpReq.status==200){//信息已经成功返回,开始处理信息

var xmlStr = xmlHttpReq.responseText;

xmlStr = parseXML(xmlStr);

//输出结果

document.getElementById("txtHint").innerHTML=xmlStr;

}else{

alert("/u6240请求的页面有异常");

}

}

}

function parseXML(xmlStr){

var key = document.getElementById("key").value.toString();

//创建DOM对象

var xmlDoc = null;

if(window.ActiveXObject){//IE

xmlDoc = new ActiveXObject("Microsoft.XMLDOM");

}else if(document.implementation&&document.implementation.createDocument){//Mozilla,Firefox,opera

xmlDoc = document.implementation.createDocument("", "", null);

xmlDoc.load(xmlStr);

var str = "";

xmlDoc.onload = function getMes(){

var tableStr = "";

if(xmlDoc!=null){

tableStr+="<table border='1'>";

var x = xmlDoc.getElementsByTagName("book");

for(i=0;i<x.length;i++){

var name = x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;

var price = x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue;

var author = x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue;

var year = x[i].getElementsByTagName("year")[0].childNodes[0].nodeValue;

if(name.indexOf(key)==-1){

continue;

}else{

tableStr+= "<tr>";

tableStr+= "<td>"+name+"</td>";

tableStr+= "<td>"+price+"</td>";

tableStr+= "<td>"+author+"</td>";

tableStr+= "<td>"+year+"</td>";

tableStr+= "</tr>";

}

}

tableStr+="</table>";

alert(tableStr);

}

str = tableStr;

}

return str;

}else{

alert("/u6d4f览器不能处理!");

}

var tableStr = "";

if(xmlDoc!=null){

xmlDoc.async = false;

xmlDoc.loadXML(xmlStr);

tableStr+="<table border='1'>";

var x = xmlDoc.getElementsByTagName("book");

for(i=0;i<x.length;i++){

var name = x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;

var price = x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue;

var author = x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue;

var year = x[i].getElementsByTagName("year")[0].childNodes[0].nodeValue;

if(name.indexOf(key)==-1){

continue;

}else{

tableStr+= "<tr>";

tableStr+= "<td>"+name+"</td>";

tableStr+= "<td>"+price+"</td>";

tableStr+= "<td>"+author+"</td>";

tableStr+= "<td>"+year+"</td>";

tableStr+= "</tr>";

}

}

tableStr+="</table>";

}

return tableStr;

}

</script>

</head>

<body>

输入关键字:<input type="text" id="key" onkeyup="search()">

图书信息:<span id="txtHint"></span>

</body>

</html>

2.books.xml

<?xml version="1.0" encoding="UTF-8"?>

<books>

<book isbn="001">

<name>《水浒传》</name>

<price>80</price>

<author>施耐庵</author>

<year>元末</year>

</book>

<book isbn="002">

<name>《西游记》</name>

<price>90</price>

<author>吴承恩</author>

<year>明代</year>

</book>

<book isbn="003">

<name>《三国演义》</name>

<price>75</price>

<author>罗贯中</author>

<year>元末</year>3

</book>

<book isbn="004">

<name>《红楼梦》</name>

<price>75</price>

<author>曹雪芹</author>

<year>清代</year>

</book>

</books>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐