Generate XML with the XML Document
Object Model
XML Document Object Model
There are four main Objects:
1. XMLDOMDocument
2. XMLDOMNode
3. XMLDOMNodeList
4. XMLDOMNamedNodeMap
In this tutorial we will see XMLDOMDocument
and XMLDOMNode objects.
XMLDOMDocument
By name, it is clear that XMLDOMDocument
is the top-level node within the XML DOM hierarchy. (Don't get confused about
root node... It's different).
Set objXMLdoc =
CreateObject("Microsoft.XMLDOM")
Table A lists some of the properties and
methods in XMLDOMDocument object.
Table
A: A selection of XMLDOMDocument's properties and methods
Method
|
Description
|
CreateAttribute
|
Creates a new attribute
|
CreateComment
|
Creates a comment node
|
CreateElement
|
Creates an element node using the
specified name
|
CreateEntityReference
|
Creates an entity reference object
|
CreateNode
|
Creates a node
|
Load
|
Loads an existing XML document
|
Save
|
Saves the XML
document
|
Property
|
Description
|
PreserveWhiteSpace
|
Indicates whether to display white space
in the XML document
|
ResolveExternals
|
Resolves namespaces, DTDs, and external
entity references at parse time
|
ValidateOnParse
|
Indicates whether the parser should
validate this document
|
DocumentElement
|
Returns the XML document's root node
|
Creating nodes
The XMLDOMDocument object provides two
methods for creating nodes or XMLDOMNode objects: CreateElement and CreateNode.
Which one you use depends on how much information you need to provide about the
node. When you use the CreateElement method, you simply provide the node's
name, such as
Set objXMLroot = objXMLdoc _
.CreateElement("ORDER_STATUS")
With the CreateNode method, you specify
the node type, the node's name, and an associated namespace. (An XML namespace
lets you create multiple XML elements with the same name within the same
document.) So, for example, to use the CreateNode method, you could use the
following code:
Set objXMLroot = objXMLdoc _
.createNode("element", _
"ORDER_STATUS", "Space1")
Once you create a node, you must add it
to the XML document. To do so, use the AppendChild method. For instance,
continuing with the previous example, you'd use
objXMLdoc.AppendChild(objXMLroot)
In addition to the AppendChild method,
the XMLDOMNode object exposes three other methods for controlling XML nodes:
ReplaceChild, RemoveChild, and InsertBefore.
Assigning node attributes
Once you create a node, you may want to
assign attributes to it, such as a unique identifier, or other property-type
information. To do so, you use the SetAttribute method. This method accepts two
parameters--the attribute name and the attribute's value. For example, the
following code creates an attribute named SHIPPING_DATASOURCE and assigns it
the value NORTH_ATLANTIC_SHIPPING:
objXMLroot.SetAttribute _
"SHIPPING_DATASOURCE", _
"NORTH_ATLANTIC_SHIPPING"
Adding child nodes
To create child nodes, you use the
CreateElement or CreateNode methods we described earlier, and then append them
to the appropriate parent node. For instance, suppose we want to create an XML
node called PUBLISHER_DISCLAIMER. To do so, we'd attach this node to the
document's root node, as in
Set objXMLChildTestNode = objXMLdoc _
.createNode("element", _
"PUBLISHER_DISCLAIMER","")
objXMLdoc.DocumentElement _
.appendChild (objXMLChildTestNode)
Assign data to a node
Sooner or later after creating all these
nodes, you're going to need to assign data to them. After all, that's the whole
point of XML. To do so, you create a node as usual, then assign the data to the
node's Text property. For example, the following code creates a XML node called
PUBLISHED_DATE and assigns the date as its value:
Set objPublishDate = objXMLdoc.CreateNode("element", _
"PUBLISHED_DATE", "")
dShippedDate = FormatDateTime(Date, 2)
objPublishDate.Text = dShippedDate
objXMLChildTestNode.appendChild(objPublishDate)
Last Updated:
29-12-2005
|