How to build a formatted XML file by using a DOMDocument
Posted on December 3, 2011
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 'XML Handling - How to build a formatted XML file by using a DOMDocument Option Explicit 'You need to set reference to "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. 'As it builds the file, the program adds text elements containing carriage 'returns and indentation to format the code nicely. 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 employees_node.appendChild xml_doc.createTextNode(vbCrLf) ' Add a comment. employees_node.appendChild xml_doc.createTextNode(" ") employees_node.appendChild xml_doc.createComment(" " & "Employee Records") employees_node.appendChild xml_doc.createTextNode(vbCrLf) ' Make some Employee elements. MakeEmployee employees_node, "Ben", "Thompson", 111 MakeEmployee employees_node, "Ion", "Corner", 222 MakeEmployee employees_node, "Srivart", "Little", 333 MakeEmployee employees_node, "Ginger", "Beelers", 444 ' 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 |