user941015
user941015

Reputation:

How to Generate a well formatted XML file using VB 6.0?

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> .

Upvotes: 5

Views: 25675

Answers (4)

Zhenya
Zhenya

Reputation: 174

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

Upvotes: 0

Deanna
Deanna

Reputation: 24283

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.

Upvotes: 1

Tomalak
Tomalak

Reputation: 338336

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.

Upvotes: 4

Ira Baxter
Ira Baxter

Reputation: 95410

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.

Upvotes: 1

Related Questions