JavaScript構造XML樹結構

來源:互聯網
上載者:User
 

用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"
%>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.