【问题标题】:VBA force SAX to encode UTF-8 and indentVBA 强制 SAX 编码 UTF-8 和缩进
【发布时间】:2016-10-06 19:20:12
【问题描述】:

我正在编写 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

【问题讨论】:

    标签: xml-parsing


    【解决方案1】:

    根据我在另一篇文章中看到的内容,您需要将.byteOrderMark 设置为truefalse,否则.encoding 将被忽略。

    【讨论】:

      猜你喜欢
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 2012-07-03
      • 2021-04-27
      • 1970-01-01
      • 2019-02-03
      • 1970-01-01
      相关资源
      最近更新 更多