ASP's approach to manipulating XML

Source: Internet
Author: User
Tags error handling

The main method and implementation of ASP to manipulate XML file on server side by XMLDOM
For small amounts of data, XML files have many advantages in retrieving updates on access.

I have tested the use of the database, the site's membership information, merchandise data information, transaction information, Web site customization information stored in three XML files, the results of the operation is very normal, feeling more than the database faster, but did not test, can not be determined.

Here are the main ways to create, query, modify, and so on XML operations

' Create a DOM object
Set Objdom=server. CreateObject ("Microsoft.XMLDOM")

' Get XML data
' Method 1 Gets the XML data of the XML file
Objdom.load ("C:/test.xml")
' Method 2 Gets the data of the XML data string
Objdom.loadxml ("<people><man name=" SD "/></people>")

' Create a Node object
Set newnode=objdom.createelement ("people")
' To the value of this node
Newnode.text= "Man"
' Add attributes to this node
Set newattribute=objdom.createnode ("attribute", "name", "")
newattribute.text= "John"
Newnode.setattributenode Newattribute
' Add child nodes to this node
Set newnodechild=objdom.createelement ("address")
Newnode.appendchild Newnodechild
' Save this Node object
Objdom.appendchild Newnode
Objdom.save ("C:/test.xml")

' Find a Node object
Set Objtofind=objdom.documentelement.selectsinglenode ("//people/man")
' Take out the node name of this node object, the node value, a property value, and all the XML
Nodename=objtofind.nodename
Nodevalue=objtofind.text
Objtofind. GetAttributeNode ("name"). NodeValue ' property value named Name

' Remove an Attribute node object
Set Objattrtofind=objdom.documentelement.selectsinglenode ("//people/man"). GetAttributeNode ("name")
' Take out the attribute name of this node, attribute value
Nodeattrname=objattrtofind.nodename
Nodeattrvalue=objattrtofind.nodevalue

' Delete a Node object
Set Objnode=objdom.documentelement.selectsinglenode ("//people/man") ' the node to be deleted
Set Objparentnode=objdom.documentelement.selectsinglenode ("//people") ' parent node of the node to be deleted
Objparentnode.removechild Objnode

' Take out a set of byte points for a node
Set Objnodes=objdom.documentelement.selectsinglenode ("//people/man"). ChildNodes
Traverse this collection
Method 1
For each element in Objnodes
Response.Write Element.nodename Byte Roll Call
Response.Write Element.text Byte point value
Next
Method 2
Domlength=objnodes.length
For i = 0 to Domlength-1
Response.Write Objnodes.childnodes (i)-nodename byte Roll Call
Response.Write Objnodes.childnodes (i). Text byte point value
Next

' Remove a collection of attributes from a node
Set Objnodes=objdom.documentelement.selectsinglenode ("//people/man"). GetAttributeNode ("name"). Attributes
Traverse this collection
For each element in Objnodes
Response.Write Element.nodename Property Name
Response.Write Element.nodevalue Property Value
Next

And so can skillfully use the XMLDOM object to manipulate the XML file, you can enjoy
By the XMLHTTP object to implement many of the functions of ASP.

re:[Transfer]asp The main method and implementation of XML file operation via XMLDOM on server side
Fei Fei, why don't you introduce the use of @, without this, a lot of things inconvenient.
XML is also known as data compression technology, as the name implies, XML can act as a database to use.
So, we can think of XML as a "small database". Why is it small? Because the function and application of XML itself is convenient, there is a certain difference with the database. So why do we use XML? Because, sometimes some of our applications, although data access, but, if the use of the database, the explicit line is somewhat inflexible and convenient. At this point, we should use XML in conjunction with it.
Since XML can be viewed as a database, the first step in its work is of course to create linked objects. (Take Asp+xml as an example)
Create a method, like a linked database, with Server.CreateObject.
The method is as follows:
Set xmldoc= Server.CreateObject ("Microsoft.XMLDOM")
Xmldoc.async=false
Xmldata= Data Source Absolute path
Xmldoc.load XMLData ' Here using the Load method for linking

Both in the XML data format is more humane, can be caused by artificial or other reasons, the data format is not legitimate, then if you continue to use, will cause the program, so we often create a good link object after the data format verification.
The method is as follows:
If Xmldoc.parseerror.errorcode<>0 Then
.... Error handling

<%
' —————————————————————-
Program Introduction: Completing the ASP language to add, delete, modify, view the specified node text in an XML document
' entry parameters: No
' exit parameters: None
' —————— ——————————
' function name: Connectxml ()
' entry parameter: filename needs to connect or Open XML filename
' exit parameter: None
' return value: connectxml= 0,xmlmorntekdocument is an object that successfully loads an XML document.
' connectxml<>0, print error message strerror
' ————————————————
Dim xmlmorntekdocument

Function connectxml (filename)
Dim strsourcefile
strSourceFile = server.mappath (filename)
Set Xmlmorntekdocument = Server.CreateObject ("Microsoft.XMLDOM")   
Xmlmorntekdocument.async = false  &NBSP
Xmlmorntekdocument.load (strsourcefile)
Connectxml=xmlmorntekdocument.parseerror.errorcode
If Xmlmorntekdocument.parseerror.errorcode<>0 then
strerror= " strerror=strerror& xmlmorntekdocument.parseerror.reason& "<br>"
strerror=strerror& xmlmorntekdocument.parseerror.url& "<br>"
Strerror=strerror&xmlmorntekdocument.parseerror.line & "<br>"
strerror=strerror&xmlmorntekdocument.parseerror.filepos& "<br>"
strerror= strerror&xmlmorntekdocument.parseerror.srctext& "<br>"
Response.Write strerror
End If
End function

‘————————————————
' Function name: Closexml ()
' Entry parameters: None
' Exit Parameters: None
‘————————————————
function Closexml (xmlmorntekdocument)
If IsObject (xmlmorntekdocument) Then
Set xmlmorntekdocument=nothing
End If
End Function

‘————————————————
' Function name: Selectxmlnodetext (elementname)
' Entry parameter: The name of the elementname element
' Exit Parameters: None
‘————————————————
function Selectxmlnodetext (elementname)
Elementname= "//" &elementname
Temp=xmlmorntekdocument.selectsinglenode (elementname). Text
selectxmlnodetext= Server.HTMLEncode (temp)

End Function

' ————————————————
' function name: Insertxmlnodetext (befelementname,elementname,elementtext)
' entry parameter: elementname The name of the inserted element
'            Befelementname inserts an element before the name of this element
'            Elementtext The text of the inserted element
' exit parameter: None
' ——————————————— -
Function Insertxmlnodetext (befelementname,elementname,elementtext)   
Dim befelement,element
Set Befelement=xmlmorntekdocument.selectsinglenode ("///" &befelementname)
Set element= Xmlmorntekdocument.createelement (elementname)
Befelement.insertbefore element,befelement.firstchild
Element.text=elementtext
End function  

‘————————————————
' Function name: Updatexmlnodetext (Elementname,newelementtext)
' Entry parameter: The name of the elementname element
' New text for newelementtext element
' Exit Parameters: None
‘————————————————
function Updatexmlnodetext (elementname,newelementtext)
Dim element
Set Element=xmlmorntekdocument.selectsinglenode ("//" &elementname)
Element.text=newelementtext
End Function

‘————————————————
' Function name: Deletexmlnodetext (elementname)
' Entry parameter: The name of the elementname element
' Exit Parameters: None
‘————————————————
function Deletexmlnodetext (elementname)
Xmlmorntekdocument.selectsinglenode ("//" &elementname). Text = ""
End Function
%>

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.