Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing MSXML to format XML output with indents and newlines

Tags:

vb6

msxml

I am using MSXML 3.0 with Visual Basic 6 to store and retrieve configuration of my application. When saving the resulting DOMDocument to a XML file the root object gets rendered as a single very long line of text:

<?xml version="1.0"?>
<!--WORKAPP 2011 Configuration file-->
<profile version="1.0"><frmPlan><left>300</left><top>300</top><width>24600</width><height>13575</height></frmPlan><preferences><text1/><text2/><text3/><background_color/><grid-major-step-x>50</grid-major-step-x><grid-major-step-y>50</grid-major-step-y></preferences></profile>

Is it possible to force MSXML to format the resulting XML file with indents and newlines?

like image 785
ezpresso Avatar asked Dec 05 '22 21:12

ezpresso


2 Answers

For such tiny files as a config the overhead of using XSL probably isn't significant anyway. The power of SAX is more important when you're dealing with large files or tons of small ones such as the server side of a Web Service - and there you probably should not be using the heavyweight DOM in the first place.

Private Sub FormatDocToFile(ByVal Doc As MSXML2.DOMDocument, _
                            ByVal FileName As String)
    'Reformats the DOMDocument "Doc" into an ADODB.Stream
    'and writes it to the specified file.
    '
    'Note the UTF-8 output never gets a BOM.  If we want one we
    'have to write it here explicitly after opening the Stream.
    Dim rdrDom As MSXML2.SAXXMLReader
    Dim stmFormatted As ADODB.Stream
    Dim wtrFormatted As MSXML2.MXXMLWriter

    Set stmFormatted = New ADODB.Stream
    With stmFormatted
        .Open
        .Type = adTypeBinary
        Set wtrFormatted = New MSXML2.MXXMLWriter
        With wtrFormatted
            .omitXMLDeclaration = False
            .standalone = True
            .byteOrderMark = False 'If not set (even to False) then
                                   '.encoding is ignored.
            .encoding = "utf-8"    'Even if .byteOrderMark = True
                                   'UTF-8 never gets a BOM.
            .indent = True
            .output = stmFormatted
            Set rdrDom = New MSXML2.SAXXMLReader
            With rdrDom
                Set .contentHandler = wtrFormatted
                Set .dtdHandler = wtrFormatted
                Set .errorHandler = wtrFormatted
                .putProperty "http://xml.org/sax/properties/lexical-handler", _
                             wtrFormatted
                .putProperty "http://xml.org/sax/properties/declaration-handler", _
                             wtrFormatted
                .parse Doc
            End With
        End With
        .SaveToFile FileName
        .Close
    End With
End Sub
like image 141
Bob77 Avatar answered Mar 15 '23 20:03

Bob77


Probably this answer will not help in your specific case, but in general it may be of use. It regards cases when the document is loaded and saved without much modification. DomDocument has preserveWhitespace property, which is initially set to False. If you set it to True before load, then it will be saved using the same indentation as the original file.

To add the indentation manually one may create text nodes and insert them to create new lines and spaces between elements, like this:

Set txt = doc.createTextNode(vbCrLf & "  ")
Call node.parentNode.insertBefore(txt, node)
like image 31
Jarekczek Avatar answered Mar 15 '23 20:03

Jarekczek