2

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

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

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

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

Sourcecode:

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

The output always looks like:

<?xml version="1.0" standalone="no"?>
FireEmerald
  • 1,002
  • 12
  • 21

1 Answers1

0

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.

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

Gives the result

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

The code base is from here: How can I pretty-print XML source using VB6 and MSXML?

joking
  • 21
  • 7