JavaScript构造XML树结构

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

用JavaScript构造XML树结构的一个例子,包括删除添加节点。

关键词:ASP, JavaScript, Java, XML

将Client端的加入的数据,形成所需要的XML树。
这种方法适合于处理Client端复杂的数据,
最后在Server端完成XML树的存储。
说白了就是XML只是一个存放临时数据的垃圾站。^_^
大家可以在这个模型基础上做扩充移动修改等功能。

下列存为EnterItem.htm
<html>
<head>
<title>
  Item Information
</title>
</head>

<body>
<div id=additem name=additem></div>
<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">
        </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=750,top=50,left=30');"><br>
</body>
</html>

<script>
var ItemNo=0;
var objDom = new ActiveXObject("MSXML.DOMDocument");     file://define a DOM object
objDom.async=false;
var objRoot = objDom.createElement("EPR");                                file://create the root
objDom.appendChild(objRoot)
var XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");       

file://-------add a new node----------
function add()
{
ItemNo++;

var objField = objDom.createElement("Item")
objDom.documentElement.appendChild(objField);

curnode=objDom.documentElement.lastChild;
var namedNodeMap =curnode.attributes;

var objattID = objDom.createAttribute("ItemNo");
objattID.text =ItemNo
namedNodeMap.setNamedItem(objattID);

var objattID = objDom.createElement("Name")
objattID.text=document.formItem.ItemName.value
curnode.appendChild(objattID)
document.formItem.ItemName.value="";

var objattID = objDom.createElement("Mount")
objattID.text=document.formItem.Mount.value
curnode.appendChild(objattID)
document.formItem.Mount.value=""

var objattID = objDom.createElement("Spec")
objattID.text=document.formItem.Spec.value
curnode.appendChild(objattID)
document.formItem.Spec.value=""

var objattID = objDom.createElement("price")
objattID.text=document.formItem.price.value
curnode.appendChild(objattID)
document.formItem.price.value=""

saveXML();
}

file://-----------Del Node-------------
function Del(DelNo)
{
var cond
cond="//.[@ItemNo='" +DelNo+"']";
var delNode=objDom.documentElement.selectSingleNode(cond)
var del=objRoot.removeChild(delNode)

saveXML();
}

file://----------save XML----------------
function saveXML()
{
XMLHTTP.open("POST","saveXML.asp",false);
XMLHTTP.send(objDom);
if(XMLHTTP.responseText.indexOf("Error:")!=-1)
    {
        alert(XMLHTTP.responseText);
  }
}
</script>

 

存为saveXML.asp
  <%@ Language=VBScript %>
  <%
Response.expires=-1
dim XMLrec
set XMLrec=server.CreateObject("microsoft.XMLdom")
XMLrec.async=false
XMLrec.load(Request)
XMLrec.save Server.MapPath("")&"EPR.XML"
%>

<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 728x15, 创建于 08-4-23MSDN */ google_ad_slot = "3624277373"; google_ad_width = 728; google_ad_height = 15; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript"><!-- google_ad_client = "pub-2947489232296736"; /* 160x600, 创建于 08-4-23MSDN */ google_ad_slot = "4367022601"; google_ad_width = 160; google_ad_height = 600; //--> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
阅读更多
个人分类: javascript delphi
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭