强制 MSXML 使用缩进和换行符格式化 XML 输出 [英] Forcing MSXML to format XML output with indents and newlines

查看:54
本文介绍了强制 MSXML 使用缩进和换行符格式化 XML 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 MSXML 3.0 和 Visual Basic 6 来存储和检索我的应用程序的配置.将生成的 DOMDocument 保存到 XML 文件时,根对象将呈现为一行很长的文本:

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>

是否可以强制 MSXML 使用缩进和换行符来格式化生成的 XML 文件?

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

推荐答案

对于像配置这样的小文件,使用 XSL 的开销可能并不重要.当您处理大文件或大量小文件(例如 Web 服务的服务器端)时,SAX 的强大功能更为重要 - 在这种情况下,您可能一开始就不应该使用重量级 DOM.

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

这篇关于强制 MSXML 使用缩进和换行符格式化 XML 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆