Common methods of DOM manipulating XML

Source: Internet
Author: User
Tags add object end key
Dom|xml (all of the DOM's methods and properties are accessible to the Object Browser of VS)

' 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

' Fetch 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 naming
 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 naming
 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

======================================================

<%
Dim Xmldoc,node
Set Xmldoc=server. CreateObject ("Msxml2.domdocument")
Xmldoc.async=false ' Asynchronous transfer
Xmldoc.loadxml "<ROOT/>"

Xmldoc.createprocessinginstruction "xml", "version= ' 1.0 ' encoding= ' gb2312 '"

Set Node=xmlcreatenode (xmldoc,xmldoc.documentelement, "user", 0, 0, "")

Xmlcreatenode Xmldoc,node, "name", 1,1, "ssm1226"
Xmlcreatenode Xmldoc,node, "name", 0, 1, "It says: Rain Man"

'*********************************************************
' Function function: Add child nodes or attributes
' Xmldoc--xml DOM Object
' xmldocobj--to add the destination node object
' elenameorattname--node name or property name
' key--0-node; 1-Genus
' iftxt--whether there is content 0-none; 1-there
' text--when iftxt=1 is effective, content
' By ' Copyright information: author:ssm1226;d ata:2003-1-2;copyright:yeboss.net
'*********************************************************
function Xmlcreatenode (xmldoc,xmldocobj,elenameorattname,key,iftxt,text)
If Key=0 Then
Dim subnode
Set Subnode=xmldoc.createelement (Elenameorattname)
If Iftxt=1 then Subnode.text=text
Xmldocobj.appendchild subnode
Set Xmlcreatenode=subnode
Set subnode=nothing
ElseIf Key=1 Then
Dim Attnode
Set Attnode=xmldoc.createattribute (Elenameorattname)
If Iftxt=1 then Attnode.text=text
XmldocObj.attributes.setNamedItem Attnode
Set Xmlcreatenode=attnode
Set attnode=nothing
End If
End Function

Response.Write Xmldoc.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.