代码之家  ›  专栏  ›  技术社区  ›  Michael Paulukonis

MSXMLWriter60不输出UTF-16编码的字节顺序标记

  •  1
  • Michael Paulukonis  · 技术社区  · 15 年前

    "How to make XMLDOMDocument include the XML Declaration?" (也可在 MSDN

    我忽略了什么?

    '' # Create and load a DOMDocument object.
    
    Dim xmlDoc As New DOMDocument60
    xmlDoc.loadXML("<doc><one>test1</one><two>test2</two></doc>")
    
    '' # Set properties on the XML writer - including BOM, XML declaration and encoding
    
    Dim wrt As New MXXMLWriter60
    wrt.byteOrderMark = True
    wrt.omitXMLDeclaration = False
    wrt.encoding = "UTF-16"
    wrt.indent = False
    
    '' # Set the XML writer to the SAX content handler.
    
    Dim rdr As New SAXXMLReader60
    Set rdr.contentHandler = wrt
    Set rdr.dtdHandler = wrt
    Set rdr.errorHandler = wrt
    rdr.putProperty "http://xml.org/sax/properties/lexical-handler", wrt
    rdr.putProperty "http://xml.org/sax/properties/declaration-handler", wrt
    
    '' # Now pass the DOM through the SAX handler, and it will call the writer
    
    rdr.parse xmlDoc
    
    '' # Let the writer do its thing
    
    Dim iFileNo As Integer
    iFileNo = FreeFile
    Open App.Path + "\saved.xml" For Output As #iFileNo
    Print #iFileNo, wrt.output
    Close #iFileNo
    

    输出如下所示:

    <?xml version="1.0" encoding="UTF-16" standalone="no"?>
    <doc><one>test1</one><two>test2</two></doc>
    

    1 回复  |  直到 8 年前
        1
  •  2
  •   AnthonyWJones    15 年前

    问题是,当您从writer的输出属性中检索值时,您将得到一个字符串。因为VB中的字符串总是UTF-16,所以不管编码是什么,都会得到UTF-16。由于字符串在VB中始终是UTF-16,因此没有任何概念认为它们需要BOM表,因此也不包括BOM表。

    编码和BOM属性仅影响编写器在使用IStream实现时编写XML的方式 到输出属性。

    尝试围绕调用修改代码以进行如下分析:-

    Dim oStream As ADODB.Stream
    Set oStream =  New ADODB.Stream
    oStream.Open
    oStream.Type = adTypeBinary
    
    wrt.output = oStream
    
    rdr.parse xmlDoc
    
    oStream.SaveToFile App.Path + "\saved.xml"
    oStream.Close
    

    这将生成所需的输出。