DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 一個在客戶端生成並使用XML的例子
一個在客戶端生成並使用XML的例子
編輯: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" onclick="add();">
<input type="button" id="btndel" name="btndel" value="Del" onclick="Del(document.all.delNo.value);">
Del No : <input type="text" size=3 name="delNo">
<input type="button" size=3 onclick="ListItem();" value="ReList All Item": >
</td>
</tr>


</TABLE>
</form>
<input type="button" id="lookXML" name="lookXML" value="look XML" onclick="window.open('EPR.xml','XML','toolbar=no,status=no,scrollbars=yes, height=400,width=500,top=50,left=30');"><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"
%>

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved