Class cXMLTextWriter Private oXML, oCurrentNode, bDocStarted, bDocClosed, mOpenElements Public PreserveEmptyNodes Private Sub Class_Initialize() Set oXML=Server.CreateObject("MSXML2.DomDocument") PreserveEmptyNodes=true mOpenElements=0 bDocStarted=false bDocClosed=true End Sub Private Sub Class_Terminate() Set oXML=Nothing End Sub Public Function WriteStartDocument(TagName) if not bDocStarted then Set oXML.documentElement=oXML.createElement(TagName) Set oCurrentNode=oXML.documentElement bDocStarted=true bDocClosed=false else err.Raise vbObjectError + 1, "cXMLTextWriter", "The document has already been started" end if End Function Public Function WriteEndDocument() if mOpenElements=0 then bDocClosed=true else err.Raise vbObjectError + 3, "cXMLTextWriter", "You will have " & mOpenElements & " open element(s). Most Recent: " & oCurrentNode.TagName end if End Function Public Function WriteStartElement(TagName) if bDocStarted and not bDocClosed then Dim oNewNode Set oNewNode=oXML.createElement(TagName) oCurrentNode.appendChild oNewNode Set oCurrentNode=oNewNode mOpenElements=mOpenElements+1 else err.Raise vbObjectError + 2, "cXMLTextWriter", "Either the document is not started or has been closed" end if End Function Public Function WriteEndElement() if bDocStarted and not bDocClosed then Set oCurrentNode=oCurrentNode.parentNode mOpenElements=mOpenElements-1 else err.Raise vbObjectError + 2, "cXMLTextWriter", "Either the document is not started or has been closed" end if End Function Public Function WriteString(Text) if bDocStarted and not bDocClosed then oCurrentNode.text = Text else err.Raise vbObjectError + 2, "cXMLTextWriter", "Either the document is not started or has been closed" end if End Function Public Function WriteAttributeString(AttributeName, AttributeValue) if bDocStarted and not bDocClosed then oCurrentNode.setAttribute AttributeName, AttributeValue else err.Raise vbObjectError + 2, "cXMLTextWriter", "Either the document is not started or has been closed" end if End Function Public Function WriteElementString(TagName, NodeText) if bDocStarted and not bDocClosed then if len(NodeText)>0 or PreserveEmptyNodes then 'only write if there is a value Dim oNewNode Set oNewNode=oXML.createElement(TagName) if len(NodeText)>0 then oNewNode.text=NodeText oCurrentNode.appendChild oNewNode end if else err.Raise vbObjectError + 2, "cXMLTextWriter", "Either the document is not started or has been closed" end if End Function Public Property Get xml() if bDocClosed Then xml=oXML.xml else err.Raise vbObjectError + 4, "cXMLTextWriter", "You must first close the document" end if End Property Public Property Get xmlObject() if bDocClosed Then Set xmlObject=oXML else err.Raise vbObjectError + 4, "cXMLTextWriter", "You must first close the document" end if End Property End Class