Construct an XML tree structure using JavaScript

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.