xml|客户端
这是一个在客户端生成并使用XML的例子,主要用途实现无双刷新页面在客户端处理复杂的数据,由两个文件构成。大家可以看一下,提出宝贵的意见。注意:使用时将saveXML.asp的路径改为GenXML.html的物理路径
-GenXML.html-
<!--
*******
'* Author hardvb
'* Support : web@webjx.com
'* Date Created : 2004/6/18 Merry ChrisMas Day
'* Date Modified:
'* Function: genrate XML Tree and Show in Client
'********
-->
<html>
<head>
<title>
Item Information
</title>
</head>
<body>
<H2>A example genrate XML Tree with JavaScript
<br>
<div id=addItem name=addItem></div> <!--the DIV use for add Item-->
<form name=formItem action="processForm.asp" method="post">
<TABLE WIDTH="60%" BORDER="2" bordercolor="orange" CELLSPACING="1" CELLPADDING="1">
<tr>
<td colspan=2><h3>Enter your Item information</h3></td>
</tr>
<tr>
<td>Item Name:</td>
<td><input type="text" name="ItemName"></td>
<tr>
<tr>
<td>Mount: </td>
<td> <input type="text" name="Mount"></td>
</tr>
<tr>
<td> Spec: </td>
<td> <input type="text" name="Spec"></td>
</tr>
<tr>
<td> price:</td>
<td><input type="text" name="price"></td>
</tr>
<tr>
<td align=center colspan=2>
<input type="button" id="btnSub" name="btnSub" value="Add" >
<input type="button" id="btndel" name="btndel" value="Del" >
Del No : <input type="text" size=3 name="delNo">
<input type="button" size=3 value="ReList All Item": >
</td>
</tr>
</TABLE>
</form>
<input type="button" id="lookXML" name="lookXML" value="look XML" ><br>
</body>
</html>
<script language="JavaScript">
file://-----global var define -------
var ItemNo=0; file://how many Items born and die
var objDom = new ActiveXObject("MSXML.DOMDocument"); file://define a DOM object
objDom.async=false;
var objRoot = objDom.createElement("EPR"); file://create the root "EPR"
objDom.appendChild(objRoot)
var xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP"); file://create the XMLHTTP object for Save XML without Refrsh Page
file://-------add a new node----------
function add()
{
ItemNo++;
var ItemSec=document.all.addItem.children.length; // how many alive Items
ItemSec++;
var objField = objDom.createElement("Item") file://create node "EPR-->Item"
objDom.documentElement.appendChild(objField);
curnode=objDom.documentElement.lastChild; file://look for last added node
var namedNodeMap =curnode.attributes;
var objattID = objDom.createAttribute("ItemNo"); file://add attribute "ItemNo "
objattID.text =ItemNo
namedNodeMap.setNamedItem(objattID);
var objattID = objDom.createElement("Name") file://create node "EPR-->Item-->name"
objattID.text=document.formItem.ItemName.value
curnode.appendChild(objattID)
document.formItem.ItemName.value="";
var objattID = objDom.createElement("Mount") file://create node "EPR-->Item-->Mount"
objattID.text=document.formItem.Mount.value
curnode.appendChild(objattID)
document.formItem.Mount.value=""
var objattID = objDom.createElement("Spec") file://create node "EPR-->Item-->Spec"
objattID.text=document.formItem.Spec.value
curnode.appendChild(objattID)
document.formItem.Spec.value=""
var objattID = objDom.createElement("price") file://create node "EPR-->Item-->Price"
objattID.text=document.formItem.price.value
curnode.appendChild(objattID)
document.formItem.price.value=""
saveXML(); file://save in Server with ASP
AddItem(); file://Show in DIV
}
file://-----------Del Node-------------
function Del(DelNo)
{
var cond
var Itno=DelNo-1
cond="Item[" +Itno+"]";
var delNode=objDom.documentElement.selectSingleNode(cond) file://choose node to Del
var del=objRoot.removeChild(delNode)
saveXML(); file://save in Server with ASP
DelItem(DelNo); file://Del in DIV
}
file://----------save XML----------------
function saveXML()
{
xmlHTTP.open("POST","saveXML.asp",false);
xmlHTTP.send(objDom);
if(xmlHTTP.responseText.indexOf("Error:")!=-1)
{
alert(xmlHTTP.responseText);
}
}
file://---------Add Item--------------------
function AddItem()
{
var ItemSec=document.all.addItem.children.length;
ItemSec++;
var addItem=document.all.addItem;
var oNewNode = document.createElement("DIV");
addItem.appendChild(oNewNode);
oNewNode.innerHTML="<Table><tr><td> ItemSec "+ ItemSec +" ItemNo "+ItemNo+"</td></tr></Table>";
}
file://---------Del Item--------------------
function DelItem(itNo)
{ var ItNum=itNo
var addItem=document.all.addItem;
var oChild=addItem.children(ItNum-1);
addItem.removeChild(oChild);
}
file://---------List Item--------------------
function ListItem()
{
var addItem=document.all.addItem;
var ItemList=0;
var oNewNode;
addItem.innerHTML=""; file://clear DIV
for ( x=0;x<objDom.documentElement.childNodes.length;x++ )
{
ItemList=x+1; file://get list NO
Itemnode=objDom.documentElement.childNodes(x) file://chose cucrry node
Itemname=Itemnode.selectSingleNode("Name");
ItemMount=Itemnode.selectSingleNode("Mount");
ItemSpec=Itemnode.selectSingleNode("Spec");
Itemprice=Itemnode.selectSingleNode("price");
oNewNode = document.createElement("DIV");
addItem.appendChild(oNewNode);
oNewNode.innerHTML="<Table WIDTH='60%' BORDER='1' bordercolor='green' CELLSPACING='1' CELLPADDING='1' ><tr><td colspan=2 align=center> ItemNo "+ItemList+"</td></tr><tr><td WIDTH='30%'>name: </td><td WIDTH='30%'>"+Itemname.text+" </td></tr><tr><td WIDTH='30%'>Item Mount: </td><td WIDTH='30%'>"+ItemMount.text+" </td></tr><tr><td WIDTH='30%'>Spec: </td><td WIDTH='30%'>"+ItemSpec.text+" </td></tr><tr><td WIDTH='30%'>price: </td><td WIDTH='30%'>"+Itemprice.text+" </td></tr></Table>";
}
}
</script>
------------saveXML.asp-----------------
<%@ Language=VBScript %>
<%
Response.expires=-1
dim xmlrec
set xmlrec=server.CreateObject("microsoft.xmldom")
xmlrec.async=false
xmlrec.load(Request)
xmlrec.save "C:\EPR.xml" '//please change Path with your IIS path
'response.write GetPath()&"\EPR.xml"
%>