5

I'm working on Visual Basic 6.0 Project and i need to generate a well formatted XML file whose looks like this:

<Myinfo>
      <FirstName>My First Name</FirstName>
      <LastName>My Last Name</LastName>
      <StreetAdd>My Address</StreetAdd>
<MyInfo>

Note: i got the job done generating the XML file, but i'm still in need for the right formatting as shown above.

The XML file i generated is formatted like in one single line like this:

<Myinfo><FirstName>My First Name</FirstName><LastName>My Last Name</LastName><StreetAdd>My Address</StreetAdd><MyInfo> .
Tomalak
  • 332,285
  • 67
  • 532
  • 628

4 Answers4

4

I've made a small XML pretty printer that works quite well:

Sub PrettyPrint(Parent As IXMLDOMNode, Optional Level As Integer)
  Dim Node As IXMLDOMNode
  Dim Indent As IXMLDOMText

  If Not Parent.ParentNode Is Nothing And Parent.ChildNodes.Length > 0 Then
    For Each Node In Parent.ChildNodes
      Set Indent = Node.OwnerDocument.createTextNode(vbNewLine & String(Level, vbTab))

      If Node.NodeType = NODE_TEXT Then
        If Trim(Node.Text) = "" Then
          Parent.RemoveChild Node
        End If
      ElseIf Node.PreviousSibling Is Nothing Then
        Parent.InsertBefore Indent, Node
      ElseIf Node.PreviousSibling.NodeType <> NODE_TEXT Then
        Parent.InsertBefore Indent, Node
      End If
    Next Node
  End If

  If Parent.ChildNodes.Length > 0 Then
    For Each Node In Parent.ChildNodes
      If Node.NodeType <> NODE_TEXT Then PrettyPrint Node, Level + 1
    Next Node
  End If
End Sub

You call it by passing in the DOMDocument object and leaving the Level parameter blank.

  • It does an in-place modification of the document.
  • You will lose all insignificant whitespace (blanks between XML elements) that might have been there.
  • It uses one tab to indent.
  • It also indents comments and processing instructions etc.
  • it will work with all versions of DOMDocument.
Dim XmlDoc as New MSXML2.DOMDocument40

' create/load your xml document

PrettyPrint XmlDoc

MsgBox XmlDoc.xml

There also is an easy way to do it via SAX.

Community
  • 1
  • 1
Tomalak
  • 332,285
  • 67
  • 532
  • 628
1

You need to count the nesting of the tags, and output indentation corresponding to the depth of nesting. Something like (forgive my bad VB6 syntax):

Int Nesting=0
Bool LastOutputWasText=false

Sub XMLIndent
   Do I=1,Nesting
     Print " ";
   End Do
End Sub

Sub XMLOutputOpenTag(String OpenTag)
    if LastOutputWasText then
      Print
    endif
    XMLIndent
    Nesting=Nesting+1
    Print OpenTag;
    LastOutputWasText=true
End Sub

Sub XMLOutputCloseTag(String CloseTag)
    ! Always call OpenTag and CloseTag with matching Tag names
    if !LastOutputWasText then
       XMLIndent
    endif
    Print CloseTag
    Nesting=Nesting-1
    LastOutputWasText=false
End Sub

Sub XMLOutputText(String Text)
    Print Text;
    LastOutputWasText=true
End Sub

OP left to revise this to write to a file or whereever he wants the result.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
1

You really should be using an XML library to create "correct" XML. It handles encoding, data formats, etc

Set XMLDoc = New DOMDocument
Set XMLRoot = XMLDoc.appendChild(XMLDoc.createElement("Myinfo"))
XMLRoot.appendChild(XMLDoc.createElement("FirstName")).Text = "My First Name"
XMLRoot.appendChild(XMLDoc.createElement("LastName")).Text = "My Last Name"
XMLRoot.appendChild(XMLDoc.createElement("StreetAdd")).Text = "My Address"

XMLDoc.xml will then output valid XML, or you can pretty print it as Tomalak suggests if you really want to.

Deanna
  • 23,876
  • 7
  • 71
  • 156
  • Nitpicking: It will generate *well-formed* XML. Whether it is valid or not is a different story. – Tomalak Apr 18 '12 at 19:55
0

There's a little improvement to Tomalak's code, I'm adding a vbNewLine after the end of the tag:

Private Sub FormatOuterXml(ByRef Parent As IXMLDOMNode, Optional ByVal Lvl As Long = 0)

    If Parent.ParentNode Is Nothing Or Parent.ChildNodes.Length = 0 Then Exit Sub

    Dim xn0 As IXMLDOMNode, id0 As IXMLDOMText, id1 As IXMLDOMText
    Set id0 = Parent.OwnerDocument.createTextNode(vbNewLine & String(Lvl, vbTab))

    If Lvl > 0 Then Set id1 = Parent.OwnerDocument.createTextNode(vbNewLine & String(Lvl - 1, vbTab))

    For Each xn0 In Parent.ChildNodes
        If xn0.NodeType = MSXML2.NODE_TEXT Then
            If LenB(xn0.Text) = 0 Then Parent.RemoveChild xn0
        ElseIf xn0.PreviousSibling Is Nothing Then
            Parent.InsertBefore id0.CloneNode(True), xn0
        ElseIf xn0.PreviousSibling.NodeType <> MSXML2.NODE_TEXT Then
            Parent.InsertBefore id0.CloneNode(True), xn0
        ElseIf xn0.NextSibling Is Nothing Then
            If Not id1 Is Nothing Then Parent.appendChild id1.CloneNode(True)
        End If
    Next xn0

    If Parent.ChildNodes.Length > 0 Then
        For Each xn0 In Parent.ChildNodes
            If xn0.NodeType <> MSXML2.NODE_TEXT Then FormatOuterXml xn0, Lvl + 1
       Next xn0
    End If

End Sub
Zhenya
  • 174
  • 1
  • 8
  • hey, it's a recursive function, not sure why it was called PrettyPrint2, I've changed the function name to FormatOuterXml – Zhenya Jul 14 '15 at 06:54