November 1999

Generate XML with the XML Document Object Model

by Scot Johnson

As you know, XML is a powerful new data structure that lets you separate Web content from presentation. Up until now, though, our articles have used pretty straightforward approaches to generating XML tags. Mostly, we've hand-coded the necessary tags and data. Fortunately, Microsoft provides an alternative to such hard-coding with the XML Document Object Model (XML DOM). This object library lets you build XML trees programmatically and provides a much more efficient way to create XML documents. For example, you can use XML DOM to provide data feeds to a variety of locations, or create XML documents from heterogeneous data sources that range from propriety data stores or direct feeds to client browsers. In this article, we'll cover a few of this type library's more relevant features, and then show you how to use it in an ASP page.

Get to know the XML Document Object Model

The XML DOM consists of four main objects: XMLDOMDocument, XMLDOMNode, XMLDOMNodeList, and XMLDOMNamedNodeMap. Each object has its own properties and methods, just like you'd expect in any object model. In this article, we'll deal primarily with the XMLDOMDocument and XMLDOMNode objects. XMLDOMDocument

The XMLDOMDocument object represents the top-level node within the XML DOM hierarchy (not to be confused with the root node within an XML document). It provides the basic foundation from which to build and manipulate XML structures. To create an XMLDOMDocument object variable, use the CreateObject command like so:


Set objXMLdoc = _
	CreateObject("Microsoft.XMLDOM")
Table A lists some of the properties and methods available once you create the XMLDOMDocument object.

Table A: A selection of XMLDOMDocument's properties and methods

Method Description
CreateAttribute Creates a new attribute
CreateCDATASection Creates a CDATA section node
CreateComment Creates a comment node
CreateElement Creates an element node using the specified name
CreateEntityReference Creates an entity reference object
CreateNode Creates a node
CreateTextNode Creates a text 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

Setting runtime options

As you can see in Table A, once you create the XMLDOMDocument object, you can then begin to create individual XML nodes. Before you do, however, you'll probably want to set the XML document's parsing runtime conditions, which are useful if you generate XML documents directly to a browser.

The runtime options give you the ability to set asynchronous downloads, provide document validation, preserve white space, and resolve external references. To prevent asynchronous downloads, set the asynch property to False. If you want to replace white space with a new line character, set the PreserveWhiteSpace property to True. The ResolveExternals property prevents the parsing engine from resolving entity references, document type definitions, or external name spaces.

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)
which would create an XML root node like the one shown in Figure A in Internet Explorer 5.0.

Figure A: We used XML DOM to create the XML document's root node.
[ Figure A ]

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"

At this point, if we generated the XML document, it might look Figure B.

Figure B: The SetAttributes method creates node properties.
[ Figure B ]

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)

Notice that we used the XMLDOMDocument's DocumentElement property to append to new node to the XML document's root node. At this point, the XML would resemble Figure C.

Figure C: To add child nodes, you simply append them to the parent node.
[ Figure C ]

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)
This results in the XML shown in Figure D. Now that we've covered a few basics, let's take a look at how you can use the XML DOM with ASP.

Figure D: We assigned the current date to PUBLISHED_DATE's Text property, then appended the node to the PUBLISHER_DISCLAIMER element.
[ Figure D ]

Use XML DOM with ASP

For our example, we'll complete the order status XML document that we started in our earlier examples. However, we'll generate it from an ASP page. We'll create a simple ASP page that lets you create the XML document with the click of a button. To do so, we'll use a simple ASP page along with a Windows Script Component, which will actually generate the XML. We'll use the Windows Script Component Wizard to generate the initial stub code. (If you don't already have this wizard, you can find it at http://msdn.microsoft.com/scripting/.)

Create the script component

To begin, launch the Windows Script Component Wizard. In the first window, enter XMLDom as the component's name, and then click Next. Make sure the VBScript and Support Active Server Pages options are selected in the second window, and then click Next twice. In the fourth window, enter CreateXML as our scripting objects one and only method. Click Next twice more, and then click Finish. When you do, the wizard creates the component and adds it to your desktop. At this point, we have the component's basic structure, or stub, but it doesn't contain any functionality, so let's add that next. To do so, double-click on the XMLDom shortcut on your desktop. When you do, Windows displays the stub code in Notepad. Notice that the component itself is an XML document. To add the CreateXML code, we'll create a sub procedure instead of a function. So, delete the lines that read


function CreateXML()
	CreateXML = "Temporary Value"
end function

In its place enter the code from Listing A.

Listing A: The CreateXML sub procedure


Sub CreateXML()
Dim objXMLdoc
Set objXMLdoc = CreateObject("Microsoft.XMLDOM")
    
objXMLdoc.async = False
objXMLdoc.validateOnParse = False
objXMLdoc.preserveWhiteSpace = False
objXMLdoc.resolveExternals = False
    
'==================================================
'=== Close Runtime conditions                   ===
'==================================================
    
'=== Create the root note
Set objXMLroot = objXMLdoc.createElement("ORDER_STATUS")
objXMLdoc.appendChild(objXMLroot)
    
objXMLroot.setAttribute "SHIPPING_DATASOURCE", _
	"NORTH_ATLANTIC_SHIPPING"
               
Set objXMLChildTestNode = objXMLdoc _ 
	.createNode("element", "PUBLISHER_DISCLAIMER", ")
objXMLdoc.documentElement _
	.appendChild (objXMLChildTestNode)
        
'=== Create the "PUBLISHED_DATE" Node
Set objPublishDate = objXMLdoc.createNode("element", _
	"PUBLISHED_DATE", ")
dShippedDate = FormatDateTime(Date, 3)
objPublishDate.Text = dShippedDate
objXMLChildTestNode.appendChild(objPublishDate)
Set objPublishDate = Nothing
    
'=== Create a Comment
Set objXMLcomment = objXMLdoc _
	.createComment("This is a comment.")
objXMLChildTestNode.AppendChild(objXMLcomment)
Set objXMLcomment = Nothing
    
'=== Create a CDataSection
Set objXMLcdata = objXMLdoc _
	.CreateCDATASection("This is a CData Section.")
objXMLChildTestNode.appendChild (objXMLcdata)
Set objXMLcdata = Nothing
    
'=== Create a Text Node
Set objXMLcdata = objXMLdoc _
	.CreateTextNode("This is a Text Node.")
objXMLChildTestNode.AppendChild(objXMLcdata)
Set objXMLcdata = Nothing

objXMLdoc.Save Server.MapPath("PubInfo.XML")
Set objXMLdoc = Nothing
end Sub

After you've entered the code, make sure to register the WSC. To do so, right-click on the XMLDom shortcut on your desktop, and then select Register from the shortcut menu. Now, let's build the ASP page that will generate and display the XML in Internet Explorer.

Build the ASP page

To create the ASP page, open your favorite HTML editor and enter the code shown in Listing B. Make sure the Window.Navigate line points to the proper folder. To see how it works, open the page in Internet Explorer. When you do, you should be greeted with a message that tells you the XML was created, and a Show button. When you click the button, IE displays the XML document shown in Figure E.

Listing B: The ShowXML ASP page


<% Option Explicit %>
<HTML>
<BODY>
<%
Dim objXMLCreate
Set objXMLCreate = Server.CreateObject("XMLDom.WSC")
objXMLCreate.CreateXML
Response.Write "<BR>XML Created...
<BR><BR>"
Set objXMLCreate = Nothing
%>

<FORM>
	<INPUT TYPE="BUTTON" VALUE="Show" 
NAME=btnShow>
</FORM>

<SCRIPT LANGUAGE="VBScript">
Sub btnShow_OnClick
	Window.Navigate "../XMLDemo/PubInfo.XML"
End Sub
</SCRIPT>
<BR>

</BODY>
</HTML>

Figure E: We used several different XML DOM methods to create the completed XML document.
[ FIgure E ]

Much, much more in the XML DOM library

As you can see in the sample code, the XML DOM contains a lot more methods and properties than we could cover here. For a complete list of the XML document object model, visit the Microsoft Web site at http://msdn.microsoft.com/xml/default.asp for all the details. Also, if you want to see an advanced example that uses the XML DOM, along with XSL and ASP, check out the stock portfolio application on display at www.i3solutions.com/onlinedemos.ASP.

Conclusion

XML is quickly taking the Web by storm. Its data modeling structure provides a much-needed addition to previously text-centric Web pages. The XML Document Object Model improves upon this great tool by letting you create XML documents programmatically. In this article, we've given you a basic introduction to the XML DOM and shown you how to use it in Active Server Pages.

Copyright © 1999, ZD Inc. All rights reserved. ZD Journals and the ZD Journals logo are trademarks of ZD Inc. Reproduction in whole or in part in any form or medium without express written permission of ZD Inc. is prohibited. All other product names and logos are trademarks or registered trademarks of their respective owners.