Friday, July 06, 2012

Formatting XML using MSXML

Here's another useful bit of code used to generate formatted XML.  This particular code - in C++ form - will eventually be incorporated into the Platypus API Service, when logging API calls is enabled.

Function PrettyPrintXML(strXML)

    Dim objReader, objWriter
    Set objReader = CreateObject("MSXML2.SAXXMLReader.6.0")
    Set objWriter = CreateObject("MSXML2.MXXMLWriter.6.0")

    objWriter.indent = True
    objWriter.standalone = False
    objWriter.omitXMLDeclaration = False
    objWriter.encoding = "utf-8"

    Set objReader.contentHandler = objWriter
    Set objReader.dtdHandler = objWriter
    Set objReader.errorHandler = objWriter

    objReader.putProperty _
        "", _
    objReader.putProperty _
        "", _

    objReader.parse strXML

    PrettyPrintXML = objWriter.output


The credit for this actually goes to Daniel Rikowski on StackOverflow.  All I did was convert it into usable VBScript.