Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
 
TitleAdd formatting to an XML file by using a DOMDocument object
DescriptionThis example shows how to add formatting to an XML file by using a DOMDocument object in Visual Basic 6. After building the unformatted document, it adds spaces and carriage returns to nicely format the document.
KeywordsXML, DOMDocument, formatted
CategoriesInternet, Files and Directories
 
Before you start, open the Project menu, select the References command, check the "Microsoft XML, v4.0" entry (or whatever version you have), and click OK.

When the user clicks Go, the program creates a new DOMDocument object. This object represents the XML document. The program uses its createElement method to make the main element, and uses the createComment method to add a comment to the file.

The code then calls the MakeEmployee subroutine to build elements. It displays the result in the Immediate window, calls FormatXmlDocument to format the document, and then displays the formatted result. The program finishes by saving the formatted result in a file.

 
Private Sub cmdGo_Click()
Dim xml_doc As New DOMDocument
Dim employees_node As IXMLDOMElement

    ' Make the Employees root node.
    Set employees_node = xml_doc.createElement("Employees")
    xml_doc.appendChild employees_node

    ' Add a comment.
    employees_node.appendChild xml_doc.createComment(" " & _
        "Employee Records ")

    ' Make some Employee elements.
    MakeEmployee employees_node, "Arthur", "Anderson", 1
    MakeEmployee employees_node, "Beatrice", "Baker", 22
    MakeEmployee employees_node, "Chas", "Chumerson", 333
    MakeEmployee employees_node, "Deb", "Deevers", 4444

    ' Format the result.
    Debug.Print xml_doc.xml
    FormatXmlDocument xml_doc
    Debug.Print xml_doc.xml

    ' Write the document.
    xml_doc.save txtFile.Text

    MsgBox "Done"
End Sub
 
Subroutine MakeEmployee adds an element to the parent node. It uses the element's setAttribute method to set the element's Id attribute. It then uses the document's createComment, createElement, and createTextNode methods to make a comment, the FirstName and LastName elements, and their text contents.
 
' Make an Employee element.
Private Sub MakeEmployee(ByVal parent_node As _
    IXMLDOMElement, ByVal first_name As String, ByVal _
    last_name As String, ByVal employee_id As Integer)
Dim employee_node As IXMLDOMElement
Dim first_name_node As IXMLDOMElement
Dim last_name_node As IXMLDOMElement

    ' Make the Employee element.
    Set employee_node = _
        parent_node.ownerDocument.createElement("Employee")
    parent_node.appendChild employee_node

    ' Add the Id attribute.
    employee_node.setAttribute "Id", Format$(employee_id)

    ' Add a comment inside the Employee.
    employee_node.appendChild _
        parent_node.ownerDocument.createComment(" " & _
        first_name & " " & last_name & " ")

    ' Add the FirstName and LastName elements.
    Set first_name_node = _
        parent_node.ownerDocument.createElement("FirstName")
    employee_node.appendChild first_name_node
    first_name_node.appendChild _
        parent_node.ownerDocument.createTextNode(first_name)

    Set last_name_node = _
        parent_node.ownerDocument.createElement("LastName")
    employee_node.appendChild last_name_node
    last_name_node.appendChild _
        parent_node.ownerDocument.createTextNode(last_name)
End Sub
 
Subroutine FormatXmlDocument simply calls subroutine FormatXmlNode to format the document's root node.

Subroutine FormatXmlNode formats a node and its descendants. First, if the node is a text node, the code does nothing.

Next the code determines whether the node contains only text nodes (or no child nodes).

Then if the node has children, the code processes the children. If the node has non-text children, the code adds a carriage return before the first child. This makes a new line after the node's opening tag .

The subroutine then calls itself recursively to format the node's children.

Next the program adds indentation before the nod's opening tag . This space comes in the parent node before this node.

If the node has non-text children, the code adds indentation after the last child. This comes before the node's closing tag .

Finally, the code adds a carriage return after this node in its parent. If the node has a sibling, the carriage return belongs before that sibling. If the node has no next sibling, then the carriage return belongs at the end of the parent's child nodes.

 
' Add formatting to the document.
Private Sub FormatXmlDocument(ByVal xml_doc As DOMDocument)
    FormatXmlNode xml_doc.documentElement, 0
End Sub

' Add formatting to this element. Indent it and add a
' carriage return before its children. Then recursively
' format the children with increased indentation.
Private Sub FormatXmlNode(ByVal node As IXMLDOMNode, ByVal _
    indent As Integer)
Dim child As IXMLDOMNode
Dim text_only As Boolean

    ' Do nothing if this is a text node.
    If TypeOf node Is IXMLDOMText Then Exit Sub

    ' See if this node contains only text.
    text_only = True
    If node.hasChildNodes Then
        For Each child In node.childNodes
            If Not (TypeOf child Is IXMLDOMText) Then
                text_only = False
                Exit For
            End If
        Next child
    End If

    ' Process child nodes.
    If node.hasChildNodes Then
        ' Add a carriage return before the children.
        If Not text_only Then
            node.insertBefore _
                node.ownerDocument.createTextNode(vbCrLf), _
                node.firstChild
        End If

        ' Format the children.
        For Each child In node.childNodes
            FormatXmlNode child, indent + 2
        Next child
    End If

    ' Format this element.
    If indent > 0 Then
        ' Indent before this element.
        node.parentNode.insertBefore _
            node.ownerDocument.createTextNode(Space$(indent)), _
                _
            node

        ' Indent after the last child node.
        If Not text_only Then _
            node.appendChild _
                node.ownerDocument.createTextNode(Space$(indent))

        ' Add a carriage return after this node.
        If node.nextSibling Is Nothing Then
            node.parentNode.appendChild _
                node.ownerDocument.createTextNode(vbCrLf)
        Else
            node.parentNode.insertBefore _
                node.ownerDocument.createTextNode(vbCrLf), _
                node.nextSibling
        End If
    End If
End Sub
 
This example shows the general technique for building XML documents. First you use the document's createXxx methods to make new elements, comments, and other objects to add to the document. You then append the new items to the element that should contain them.

For information about XML programming in Visual Basic .NET, see my book Visual Basic .NET and XML.

 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated