Using MSXML3.0 with VB 6.0 - Creating an XML Document using VB 6.0
(Page 3 of 5 )
In this section I will describe the steps you need to take in creating a document such as the one shown in the next paragraph.
<?xml version="1.0"?><!--Processing Instruction-->
<wclass> <!--Root of the XML Doc-->
<!--Next line is a comment-->
<!--My students who took web programming class with me-->
<student id="1"><!-- student node-->
<name>Linda Jones</name> <!-- Child of Student--->
<legacySkill>Access, VB5.0</legacySkill><!-- Child of Student--->
</student>
<student id="2">
<name>Adam Davidson</name>
<legacySkill>Cobol, MainFrame</legacySkill>
</student>
<student id="3">
<name>Charles Boyer</name>
<legacySkill>HTML, Photoshop</legacySkill>
</student>
<student id="4">
<name>Charles Mann</name>
<legacySkill>Cobol, MainFrame</legacySkill>
</student>
</wclass>]
Creating the root, a comment and a child node
The XML document will be built bit by bit so that the process is easy to understand, especially if the reader is a first-timer. The document above is built line by line starting from the beginning. Every XML document has a processing instruction at the very top of the document. The document must have a root element. The document may or may not have a comment, but the object model has the necessary method to create one. The code listing shown allows you to create an XML document with the root node "wroot," an element "comment" which is a child of the root, and an element "student" which is also a child of the root.
Private Sub Command1_Click()
Dim xmldoc As DOMDocument30
Dim ProcInstr As IXMLDOMProcessingInstruction
Dim rootElement As IXMLDOMElement
Dim aElement As IXMLDOMElement
'Creating DOM Document object
Set xmldoc = New DOMDocument30
'this adds the processing instruction
'the first line in an XML document <?xml version="1.0"?>
Set ProcInstr = xmldoc.createProcessingInstruction("xml",
"version=""1.0""")
xmldoc.appendChild ProcInstr
'Create the root element
Set rootElement = xmldoc.createElement("wroot")
Set xmldoc.documentElement = rootElement
'Creating comment node
Set comElement = xmldoc.createComment ("My students who took web
programming
class with me")
'add the comment node after the root
rootElement.appendChild comElement
'Create the node student
Set aElement = xmldoc.createElement("student")
'add the student node to the root
rootElement.appendChild aElement
'Saving the xml document to c:testWebStudents.xml
xmldoc.save "c:testWebStudents.xml"
WebBrowser1.Navigate2 ("c:testWebStudents.xml")
End Sub
When this code is executed by clicking the button labeled "Display the XML Document," the displayed form appears as shown, where a web browser control on the form displays the saved XML. We have added the "student" node, but we need to add the attribute to the student node as well as the details for the student whose "id" =1. Many objects were created and they need to be closed out; this is not shown in the code. The Web Browser control's Navigate2() method shows the XML document.

Next: Adding a child node to the student node >>
More XML Articles
More By Jayaram Krishnaswamy