Project: VBScript XMLTextWriter

I was writing yet another ASP page that returns XML when i thought to my self there has to be an easier way. When i first wrote pages like this i would just set the Response.ContentType="text/xml" and Response.Write an XML document. I soon ran into problems with encoding special characters and missing closing tags. Then i tried building a file using the DOM feature of the Microsoft parser. That involved a lot of oXML.createElements and .appendChilds and then i ran into a glut of objects to keep track of.

With my last VB.NET project, i encountered an object which would quickly let me write out an XML file and i didn't have to worry about encoding or the DOM. It was the XMLTextWriter Object. I wanted something just as easy to use in Classic ASP and this is what i came up with.

Don't get me wrong, this class isn't optimized for speed as the .NET equivalent is; It simply hides all the DOM work and makes the VBScript code to generate an XMLDocument a bit cleaner. It actually does create the DOM in memory rather than trying to properly manipulate a string. This method should ensure you output a xalid XML document above all else. Here's how you might call it...

Dim oXMLTextWriter
Set oXMLTextWriter=New cXMLTextWriter
With oXMLTextWriter
   .WriteStartDocument "Sample"
   .WriteStartElement "NodeOne"
   .WriteStartElement "SubNodeOne"
   .WriteAttributeString "IsNode","Yes"
   .WriteString "Read & Write Node<yea!>"
   .WriteElementString "NodeTwo","Node2Text"
End With
Response.write Server.HTMLEncode(oXMLTextWriter.xml)

The result looks something like this:

<SubNodeOne IsNode="Yes">
Read &amp; Write Node&lt;yea!&gt;</SubNodeOne>



returns the xml text of the document you created. Musts be called after WriteEndDocument(). [sXMLString=oXMLTextWriter.xml]
returns the DOMDocument of the document you created. Musts be called after WriteEndDocument(). [Set oXML=oXMLTextWriter.xmlObject]
Determines wether or not to write out an empty node if you use WriteNodeString with a zero-length value [true(default) or false]


begins an xml document with a root node of the given TagName
ends xml document. Must be called before you can access .xml or .xmlObject
begins a node of type TagName
closes the open node
writes out a text value to the current open node
WriteAttributeString(AttributeName, Text)
adds an attribute of type AttribiteName with a value of Text to the current open node
WriteElementString(TagName, Text)
append node of type TagName with text value of Text to current open node (does not change current node.) No not use this if you need to add subnodes or attributes to this node.

As you can see i didn't copy all of the properties and methods over exactly; just the ones i thought i would most likely use. There are also other error handling measures that should be taken that i've left out for simplicity.