ASP: modify the content of an XML file Node

Source: Internet
Author: User
Editbook. asp
1. The input parameter ID is used to modify the subscript position of the node in the XML file.
<%
Id = request ("ID ")
If isnumeric (ID) = false or isnull (ID) then
Response. Write ("the parameter is incorrect. Please return it! ")
Response. End
End if

Isedit = request ("isedit ")
If isedit = "" Or isnull (isedit) then
Strsourcefile = server. mappath ("./") & "/books. xml"
'Obtain the path of the XML file. The path varies depending on your virtual directory.
Set objxml = server. Createobject ("Microsoft. freethreadedxmldom ")
'Create an XML object with a free thread
Objxml. Load (strsourcefile) 'reads XML files into memory
Set objrootsite = objxml.doc umentelement. selectsinglenode ("// books ")

Bookid = objrootsite. childnodes. Item (ID). getattribute ("ID ")
Title = objrootsite. childnodes. Item (ID). childnodes. Item (0). Text
Author = objrootsite. childnodes. Item (ID). childnodes. Item (1). Text
%>
<Form name = "form1" method = "Post" Action = "? Isedit = edit & id = <% = ID %> ">
Bookid:
<Input type = "text" name = "bookid" value = "<% = bookid %>" size = "10" readonly = "true">
& Nbsp;
Booktitle:
<Input type = "text" name = "title" value = "<% = title %>" size = "40">
& Nbsp;
Author:
<Input type = "text" name = "bookauthor" value = "<% = author %>" size = "40">
<Input type = "Submit" value = "modify">
</Form>
<%
End if
If isedit = "edit" then
Bookid = request ("bookid ")
Title = request ("title ")
Author = request ("bookauthor ")
Strsourcefile = server. mappath ("./") & "/books. xml"
'Obtain the path of the XML file. The path varies depending on your virtual directory.
Set objxml = server. Createobject ("Microsoft. xmldom ")
'Create an XML Object
Objxml. Load (strsourcefile)
'Read XML files into memory
Set objrootlist = objxml.doc umentelement. selectsinglenode ("// books ")
Response. Write ("51th line ID:" & ID)
Set rootoldnode = objrootlist. childnodes. Item (ID)
Brstr = CHR (13) & CHR (10) & CHR (9)

Newxmlnode = brstr & "<book id =" & bookid & "" sortid = "" A "& bookid &" ">" & vbnewline &_
"<Title>" & Title & "</title>" & vbnewline &_
"<Author> author" & author & "</author>" & vbnewline &_
"</Book>" & vbnewline

'Create an XML Segment Based on the obtained data
Set objxml2 = server. Createobject ("Microsoft. xmldom ")
'Create a New XML Object
Objxml2.loadxml (newxmlnode)
'Read XML snippets into memory
Set rootnewnode1_objxml2.doc umentelement
'Get the root node of objxml2
Objrootlist. replaceChild rootnewnode, rootoldnode
'Modify test. xml
Objxml. Save (strsourcefile)
'Store the test. xml file (because test. XML is not stored and only updated in memory)
Set objxml = nothing
Set objxml2 = nothing
Response. Redirect ("books. asp ")
Response. End

End if
%>

2. How to modify the parameter ID when specifying the value of the node's property ID
<%
Id = request ("ID ")
If isnumeric (ID) = false or isnull (ID) then
Response. Write ("the parameter is incorrect. Please return it! ")
Response. End
End if

Isedit = request ("isedit ")
If isedit = "" Or isnull (isedit) then
Strsourcefile = server. mappath ("./") & "/books. xml"
'Obtain the path of the XML file. The path varies depending on your virtual directory.
Set objxml = server. Createobject ("Microsoft. freethreadedxmldom ")
'Create an XML object with a free thread
Objxml. Load (strsourcefile) 'reads XML files into memory
 
Set editobj = objxml. selectsinglenode ("// book [@ ID = '" & ID & "']")
Bookid = editobj. getattribute ("ID ")
Sortid = editobj. getattribute ("sortid ")
Title = editobj. childnodes. Item (0). Text
Author = editobj. childnodes. Item (1). Text

%>
<Form name = "form1" method = "Post" Action = "? Isedit = edit & id = <% = ID %> ">
Bookid:
<Input type = "text" name = "bookid" value = "<% = bookid %>" size = "10" readonly = "true">
& Nbsp;
Sortid:
<Input type = "text" name = "sortid" value = "<% = sortid %>" size = "10" readonly = "true">
<Br/>
Booktitle:
<Input type = "text" name = "title" value = "<% = title %>" size = "40">
<Br/>
Author:
<Input type = "text" name = "bookauthor" value = "<% = author %>" size = "40">
<Br/>
<Input type = "Submit" value = "modify">
</Form>
<%
End if
If isedit = "edit" then
Bookid = request ("bookid ")
Title = request ("title ")
Author = request ("bookauthor ")
Strsourcefile = server. mappath ("./") & "/books. xml"
'Obtain the path of the XML file. The path varies depending on your virtual directory.
Set objxml = server. Createobject ("Microsoft. xmldom ")
'Create an XML Object
Objxml. Load (strsourcefile)
'Read XML files into memory

Set editobj = objxml. selectsinglenode ("// book [@ ID = '" & ID & "']")
Editobj. Attributes. Item (1). value = bookid
Editobj. childnodes. Item (0). Text = title
Editobj. childnodes. Item (1). Text = author

''Modify test. xml
Objxml. Save (strsourcefile)
'Store the test. xml file
Set objxml = nothing

Response. Redirect ("books. asp ")
Response. End
End if
%>

Books. xml
<? XML version = "1.0" encoding = "gb2312"?>
<Books>
<Book id = "4" sortid = "A4">
<Title> author4aa </title>
<Author> author4 </author>
</Book>
<Book id = "5" sortid = "A5">
<Title> author55 </title>
<Author> author5 </author>
</Book>
<Book id = "6" sortid = "A6">
<Title> booktitle </title>
<Author> author6 </author>
</Book>
</Books>

Related Article

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.