2

I'm totally new to XML; I just started to study something for my culture using VB6 (! I know... !), without a real goal at the moment. In my first attempt I got a surprising good result, but there is a minor problem which only impact on the visual output. The first line of the XML file is a standard preamble: ?xml version="1.0" encoding="iso-8859-1"?. The second line is the node DB info and it should follow the preamble on a new line, in this way:

<?xml version="1.0" encoding="iso-8859-1"?>
<DB>Created on 15/06/2021</DB>

But instead this second line continues on the first. I tried to insert a text node with a newline (as seen on this site), but I get an error, which translated, say something as: "'impossible to execute the operation with a node of type PCDATA". My code is:

Sub MakeDB()
Dim dom As MSXML2.DOMDocument60
Dim root As IXMLDOMElement
Dim node As MSXML2.IXMLDOMNode

   Set dom = New MSXML2.DOMDocument60
   dom.async = False
   dom.preserveWhiteSpace = True
   
   Set node = dom.createProcessingInstruction("xml", "version='1.0' encoding='iso-8859-1'" + vbNewLine)
   dom.appendChild node
   Set node = Nothing
'the following lines raise the error 
   Set node = dom.createTextNode(vbNewLine)
   dom.appendChild node
   Set node = Nothing

' Create the first element.
    Set root = dom.createElement("DB")   
    Set node = dom.createTextNode("Created on " + CStr(Date))
    root.appendChild node
    Set node = Nothing
    dom.appendChild root
    ...

I realize that this is not a real problem, but I would like to understand what happens.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Orionis
  • 983
  • 7
  • 11

2 Answers2

1

Don't be concerned with that newline between the XML declaration and the document element when creating the content.

You can control whether or not to have newlines or output as one big line with serialization option of the Writer:

https://learn.microsoft.com/en-us/previous-versions/windows/desktop/ms764689(v=vs.85)

Sets whether to indent output. When set to True, this property indents output for "pretty printing" to reflect the nesting structure of the document.

Boolean. Read/write. The default is False. The general rules of indenting elements are: XML headers and root elements start on a new line with a zero indent; element content, including leading and trailing white spaces, is not changed in any way.

oMXXMLWriter.indent = True 
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • I'm using [this SO solution](https://stackoverflow.com/questions/6405236/forcing-msxml-to-format-xml-output-with-indents-and-newlines) for years. – Hel O'Ween Jun 16 '21 at 13:31
  • I'm not concerned, but the two lines joined togher are really not acceptable, I seen a lot of XML documents well and totally formatted. I wonder why I can not find a way to keep them separated. The declaration node is destroyed in code so why VB6 still consider it still accessible? The Writer you refer is the MSXML2.MXHTMLWriter60? I discovered it from your answer and have to understande what is and what it does. – Orionis Jun 16 '21 at 16:09
0

I encountered the same issue as a novice to VBA. On the advice of those who answered this question, I adapted the solution from this topic to use MSXML2.MXXMLWriter60 with indentation options/pretty print on serialization.

It ended up looking something like this:

But this ended up not working since the Processing Instructions i.e. got completely excluded on serialization. Maybe someone knows a property setting with MSXML2.SAXXMLReader60 that can help?

Public Function SaveDom(dom As DOMDocument60, filename As String)
    Dim oWriter, oReader, oFSO, oTextStream
    Set oFSO = CreateObject("Scripting.FileSystemObject")
    Set oTextStream = oFSO.CreateTextFile(filename, True)

    Set oWriter = New MSXML2.MXXMLWriter60
    oWriter.Indent = True
    oWriter.omitXMLDeclaration = True

    Set oReader = New MSXML2.SAXXMLReader60
    oReader.contentHandler = oWriter
    oReader.Parse dom
    oTextStream.Write oWriter.output
    oTextStream.Close

End Function

Sub CallingSub
    Dim dom As DOMDocument60

    ...
    SaveDom dom, sPathXML & sName
End Sub
yffu
  • 31
  • 4