VBA强制SAX编码UTF-8和缩进

时间:2016-06-07 03:33:56

标签: xml-parsing

我正在编写VBA来导出xml。 我使用SAXXMLReader因为我需要相当缩进的输出。

这就是我希望声明的样子:

<?xml version="1.0" encoding="UTF-8"?>

结果如下:

<?xml version="1.0" encoding="UTF-16" standalone="no"?>

为什么SAX忽略我的编码选择,如何强制它使用8。

Sub XML_Format_Indent_Save(xmlDoc1 As MSXML2.DOMDocument60, strOutputFile As String)
    Dim xmlWriter As MSXML2.MXXMLWriter60
    Dim strWhole As String

    Set xmlWriter = CreateObject("MSXML2.MXXMLWriter")
    xmlWriter.omitXMLDeclaration = False
    xmlWriter.Indent = True
    xmlWriter.Encoding = "utf-8"
    With CreateObject("MSXML2.SAXXMLReader")
        Set .contentHandler = xmlWriter
        .putProperty "http://xml.org/sax/properties/lexical-handler", xmlWriter
        .Parse xmlDoc1
    End With
    strWhole = xmlWriter.output
    ExportStringToTextFile strOutputFile, strWhole
End Sub

1 个答案:

答案 0 :(得分:0)

根据我在其他帖子中阅读的内容,您需要将.byteOrderMark设置为truefalse,否则会忽略.encoding