asp查询xml的代码,实现了无刷新、模糊查询功能_asp技巧
<html>
<head>
<title>不刷新页面查询的方法</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<script language="javascript">
<!--初始化,将数据岛中数据装入列表框中-->
function loadinsel()
{
var employeeid,employeelastname; //分别存放雇员ID和雇员名字
root=document.all.xmlemployees.childNodes.item(0); //返回第一个元素--employee
for(i=0;i<root.childNodes.length;i++){
getnode=root.childNodes(i); //得到empolyee的一个子节点
employeeid=root.childNodes(i).getAttribute("emid");//得到雇员ID
for(j=0;j<getnode.childNodes.length;j++){
employeeinf=getnode.childNodes(j).nodeName;
if(employeeinf=="lastname"){
employeelastname=getnode.childNodes(j).text; //得到雇员名字
}
}
//将得到的employeeid和employeelastname写进select中
if(employeeid!="" && employeelastname!=""){
option1=document.createElement("option");
option1.text=employeelastname;
option1.value=employeeid;
employeelist.add(option1);
}
}
}
<!--初始化,从数据岛中检索数据,装入列表框中-->
function findemployee(){
var employeelastname,employeeid; //分别存放雇员名字和雇员ID
employeelastname="";
employeeid="";
findtext=window.findcontent.value; //得到检索条件
//清除列表框
employeecount=employeelist.length
for(i=employeecount-1;i>=0;i--){
employeelist.remove(i);
}
root=window.xmlemployees.childNodes(0);
for(i=0;i<root.childNodes.length;i++){
getitem=root.childNodes(i); //得到empolyee的一个子节点
employeeid=root.childNodes(i).getAttribute("emid"); //得到雇员ID
for(j=0;j<getitem.childNodes.length;j++){
if(getitem.childNodes(j).nodeName=="lastname"){
employee_temp=getitem.childNodes(j).text;
if(employee_temp.indexOf(findtext)!=-1){ //查找匹配项
employeelastname=employee_temp; //找到名字匹配的雇员
}
}
}
//将符合条件的雇员信息写进select中
if(employeeid!="" && employeelastname!=""){
option1=document.createElement("option");
option1.value=employeeid;
option1.text=employeelastname;
window.employeelist.add(option1);
employeeid="";
employeelastname="";
}
}
}
</script>
<body bgcolor="#FFFFFF" text="#000000" onload="javascript:loadinsel()">
<table width="80%" border="1">
<tr>
<td> 请输入查询条件:
<input type="text" name="findcontent">
<input type="button" name="Submit" value="查找" onclick="javascript:findemployee()">
</td>
</tr>
<tr>
<td> 查询结果:
<select name="employeelist">
</select>
</td>
</tr>
</table>
<?xml version="1.0" encoding="gb2312"?>
<%
servername="wyb" 服务器名
user="sa" 用户名
pw="" 用户密码
databasename="northwind" 数据库名
set conn=server.CreateObject("adodb.connection")
conn.Open "DRIVER=SQL Server;SERVER="&servername&";UID="&user&";pwd="&pw&";DATABASE="&databasename
set rs=server.CreateObject("adodb.recordset")
sql="Select employeeid,lastname from employees order by employeeid"
rs.Open sql,conn%>
<!--将数据库中信息放入数据岛中-->
<xml id="xmlemployees">
<employee>
<%do while not rs.eof%>
<employeeitem emid="<%=rs("employeeid")%>">
<lastname><%=rs("lastname")%></lastname>
</employeeitem>
<%rs.movenext%>
<%loop%>
</employee> </xml>
<%rs.close
set rs=nothing
%>
</body>
</html>