|
|
Title | Build an XML file by using a DOMDocument object |
Description | This example shows how to build an XML file by using a DOMDocument object in Visual Basic 6. It adds elements to the document and then uses its Save method to save the result to the file and its xml property to display the result. |
Keywords | XML, DOMDocument |
Categories | Internet, 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 uses the document's Save method to write the document into a file and uses the document's xml property to display the document's XML text in the Immediate window.
|
|
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
' Write the document.
xml_doc.save txtFile.Text
Debug.Print xml_doc.xml
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 createElement and createTextNode methods to make 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 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
|
|
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.
Note that the result is an XML file that is all strung together on a single line of text. That's fine for a program reading the file but is hard for a human to read. For an example that builds a nicely formatted file, see the example Build a formatted XML file by using a DOMDocument object.
For information about XML programming in Visual Basic .NET, see my book Visual Basic .NET and XML.
|
|
|
|
|
|