gpt4 book ai didi

vb6 - 强制 MSXML 使用缩进和换行符格式化 XML 输出

转载 作者:行者123 更新时间:2023-12-04 16:01:49 25 4
gpt4 key购买 nike

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

<?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 文件?

最佳答案

对于像配置这样的小文件,使用 XSL 的开销可能并不重要。当您处理大文件或大量小文件(例如 Web 服务的服务器端)时,SAX 的强大功能更为重要 - 在那里您可能首先不应该使用重量级 DOM。

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

关于vb6 - 强制 MSXML 使用缩进和换行符格式化 XML 输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6405236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com