MXXMLWriter60忽略编码属性 [英] MXXMLWriter60 ignores encoding property

查看:54
本文介绍了MXXMLWriter60忽略编码属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 MXXMLWriter60 SAXXMLReader60 结合使用以缩进输出并添加正确的编码标签.

I'm using a MXXMLWriter60 in combination with a SAXXMLReader60 to indent the output and add the correct encoding tag.

输出内容缩进了,但是似乎总是忽略了encoding属性.为什么?

The output content is indented but it seems like the encoding property is always ignored. Why?

通过DOMDocument60.Load()加载的输入看起来像:

Input, loaded via DOMDocument60.Load(), looks like:

<?xml version="1.0" encoding="iso-8859-15"?>

源代码:

Private Sub SaveXmlAs(ByVal thisDOMDocument60 As DOMDocument60, ByVal thisEncoding As String, ByVal thisDestinationPath As String)

    ' Set properties on the XML writer - including BOM, XML declaration and encoding
    Dim xmlWriter As New MXXMLWriter60
    With xmlWriter
        .Encoding = "iso-8859-15"
        '.Version = "1.0"" encoding=""iso-8859-15" Hacky solution like hell...
        .byteOrderMark = True
        .omitXMLDeclaration = False
        .indent = True
    End With

    ' Set the XML writer to the SAX content handler.
    Dim xmlReader As New SAXXMLReader60
    With xmlReader
        Set .contentHandler = xmlWriter
        Set .dtdHandler = xmlWriter
        Set .errorHandler = xmlWriter

        ' Now pass the DOM through the SAX handler, and it will call the writer
        .Parse thisDOMDocument60
    End With

    ' Let the writer do its thing
    Open thisDestinationPath For Output As #1
        Print #1, xmlWriter.output
    Close #1
End Sub

输出始终如下所示:

<?xml version="1.0" standalone="no"?>

推荐答案

我知道这个问题已经很老了,但是我遇到了同样的问题,最后我找到了一个我想分享的可行解决方案……也许有帮助其他人面临同样的问题.

I know that this question is quite old, but I stumpled across the same issue and finaly I found a working solution I like to share... maybe it helps others facing the same problem.

将具有匹配编码定义的ADODB-Stream对象用作MXXMLWriter60的输出目标会导致预期的xml标头.

Using a ADODB-Stream object as output target for the MXXMLWriter60 with matching encoding definitions results in the expected xml header.

Sub prettyPrintXML()

Dim stream As Object
Dim reader As New SAXXMLReader60
Dim writer As New MXXMLWriter60
Dim filename As String

Set stream = CreateObject("ADODB.Stream")
With stream
    .Type = 2
    .Charset = "iso-8859-1"
    .Open
End With

With writer
    .indent = True
    .omitXMLDeclaration = False
    .Encoding = "iso-8859-1"
    .output = stream
End With

Set reader.contentHandler = writer
Set reader.errorHandler = writer

Call reader.putProperty("http://xml.org/sax/properties/declaration-handler", writer)
Call reader.putProperty("http://xml.org/sax/properties/lexical-handler", writer)

Call reader.Parse("<rootElement><levelOneItemOne><levelTwoItemOne></levelTwoItemOne></levelOneItemOne><levelOneItemTwo><levelTwoItemOne></levelTwoItemOne><levelTwoItemTwo></levelTwoItemTwo></levelOneItemTwo></rootElement>")

filename = ThisWorkbook.Path & "\prettyPrint.xml"
stream.SaveToFile filename, 2
stream.Close

End Sub

给出结果

<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<rootElement>
    <levelOneItemOne>
        <levelTwoItemOne/>
    </levelOneItemOne>
    <levelOneItemTwo>
        <levelTwoItemOne/>
        <levelTwoItemTwo/>
    </levelOneItemTwo>
</rootElement>

代码库位于此处:如何使用VB6和MSXML漂亮地打印XML源?

这篇关于MXXMLWriter60忽略编码属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆