Class: XMLFileCreate in Category XML Files : XML Files from Total Visual SourceBook

Create an XML file using the DOMDocument MSXML.DLL object with encoding to support Unicode from VBA and VB6.

Easily specify the root, add items, add elements (nodes), and add items and elements to elements.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the CXMLFileCreate class.
InitializeDocument Method Set file header with its encoding type.
AddRootElement Method Create root node.
AddElementItem Method Add an element to an existing node (element).
SaveDocument Method Saves XML data to disk.
' Example of CXMLFileCreate
' To use this example, create a new module and paste this code into it.
' Then run the procedure by putting the cursor in the procedure and pressing:
'    F5 to run it, or
'    F8 to step through it line-by-line (see the Debug menu for more options)

Private Sub Example_CXMLFileCreate()
  ' Comments: Example of using the CXMLFileCreate class to create XML files from VBA and VB6.
  '           Create an XML file that includes a list of nations, states, and counties.
  '           The example shows how to create the file header, elements, and add items or elements to elements.

  Const cstrFileName As String = "C:\Total Visual SourceBook 2013\Samples\XMLTest.xml"
  Const cstrState As String = "State"
  Const cstrProvince As String = "Province"

  Dim clsXML As New CXMLFileCreate
  Dim XMLelement As IXMLDOMElement
  Dim XMLItem As IXMLDOMElement
  Dim XMLItemStates As IXMLDOMElement
  Dim XMLItemCounties As IXMLDOMElement

  With clsXML
    ' Intialize with the root name (only one root per XML document) and encoding type
    Call .InitializeDocument("Nations", enmXML_UTF8)

    ' Create an element off the root. Keep the element object to add other items to it
    Set XMLelement = .AddRootElement("Country")

    ' Create two values for the first element. No need to keep the return object since additional items are not added to them.
    Call .AddElementItem(XMLelement, "Name", "", "United States")
    Call .AddElementItem(XMLelement, "Capital", "", "Washington, DC")

    ' For states, we get the element so we can add individual states to it
    Set XMLItemStates = .AddElementItem(XMLelement, "States", "Name", cstrState)

    ' Add states to the states item with each state's name and capital
    Set XMLItem = .AddElementItem(XMLItemStates, cstrState)
    Call .AddElementItem(XMLItem, "Name", "", "California")
    Call .AddElementItem(XMLItem, "Capital", "", "Sacramento")

    Set XMLItem = .AddElementItem(XMLItemStates, cstrState)
    Call .AddElementItem(XMLItem, "Name", "", "New York")
    Call .AddElementItem(XMLItem, "Capital", "", "Albany")

    Set XMLItem = .AddElementItem(XMLItemStates, cstrState)
    Call .AddElementItem(XMLItem, "Name", "", "Massachusetts")
    Call .AddElementItem(XMLItem, "Capital", "", "Boston")

    Set XMLItem = .AddElementItem(XMLItemStates, cstrState)
    Call .AddElementItem(XMLItem, "Name", "", "Texas")
    Call .AddElementItem(XMLItem, "Capital", "", "Austin")

    Set XMLItem = .AddElementItem(XMLItemStates, cstrState)
    Call .AddElementItem(XMLItem, "Name", "", "Virginia")
    Call .AddElementItem(XMLItem, "Capital", "", "Richmond")

    ' Add counties within Virginia
    Set XMLItemCounties = .AddElementItem(XMLItem, "Counties")
    Call .AddElementItem(XMLItemCounties, "County", "", "Alexandria")
    Call .AddElementItem(XMLItemCounties, "County", "", "Fairfax")
    Call .AddElementItem(XMLItemCounties, "County", "", "Prince William")

    ' ------------------------------------------
    ' Create a new root node for another country
    Set XMLelement = .AddRootElement("Country")
    Call .AddElementItem(XMLelement, "Name", "", "Canada")
    Call .AddElementItem(XMLelement, "Capital", "", "Ottawa")
    Set XMLItemStates = .AddElementItem(XMLelement, "States", "Name", cstrProvince)

    ' Add Provinces to the states item
    Set XMLItem = .AddElementItem(XMLItemStates, cstrProvince)
    Call .AddElementItem(XMLItem, "Name", "", "Alberta")
    Call .AddElementItem(XMLItem, "Capital", "", "Edmonton")

    Set XMLItem = .AddElementItem(XMLItemStates, cstrProvince)
    Call .AddElementItem(XMLItem, "Name", "", "British Columbia")
    Call .AddElementItem(XMLItem, "Capital", "", "Victoria")

    Set XMLItem = .AddElementItem(XMLItemStates, cstrProvince)
    Call .AddElementItem(XMLItem, "Name", "", "Ontario")
    Call .AddElementItem(XMLItem, "Capital", "", "Toronto")

    Call .SaveDocument(cstrFileName)
  End With

  Set clsXML = Nothing

End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!

View all FMS products for Microsoft Access All Our Microsoft Access Products


Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info




Free Product Catalog from FMS