An example of constructing an XML tree structure using JavaScript, including deleting and adding nodes.
Key words: Asp, JavaScript, Java, XML
Add data to the client to form the required XML tree.
This method is suitable for processing complex data on the client,
Finally, the XML tree is stored on the server side.
To put it bluntly, XML is just a garbage station that stores temporary data. Pai_^
You can expand mobile/modify functions based on this model.
The column below is 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> </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.doc umentelement. appendchild (objfield );
Curnode1_objdom.doc umentelement. 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 delnodeappsobjdom.doc umentelement. 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>
Save as 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"
%>