我使用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>
Run Code Online (Sandbox Code Playgroud)
是否可以强制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
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
16251 次 |
最近记录: |