Using MSXML3.0 with VB 6.0 - Adding a child node to the student node
(Page 4 of 5 )
You will create a new element by creating an IXMLDOMElement, and you will add it to the appropriate Element (in this case the student element) using the appendChild () method as shown by the highlighted portion in the following snippet.
Private Sub Command1_Click()
Dim xmldoc As DOMDocument30
Dim ProcInstr As IXMLDOMProcessingInstruction
Dim rootElement As IXMLDOMElement
Dim aElement As IXMLDOMElement
Dim cElement As IXMLDOMElement
Dim dElement As IXMLDOMElement
'Creating DOM Document object
Set xmldoc = New DOMDocument30
'this adds the processing instruction
'the first line in an XML document
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")
'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
'create a child element, 'name' for the student
Set cElement = xmldoc.createElement("name")
'add a value for this node, 'Linda Jones'
cElement.nodeTypedValue = "Linda Jones"
'cElement is child of aElement
'add the cElement as a child of aElement
aElement.appendChild cElement
'Saving the xml document to c:testWebStudents.xml
xmldoc.save "c:testWebStudents.xml
"
WebBrowser1.Navigate2 ("c:testWebStudents.xml")
End Sub
The output of this code as seen in the browser is shown in the following picture.

Adding attributes to the student node
Adding an attribute consists of declaring an object of the type IXMLAttribute. This is followed by creating this object using the CreateAttribute() method. Attributes are name value pairs; the name is assigned during creation. Next you use the attributes' text property to associate the name with a value. Finally associate this attribute with the element you want as shown in the following code listing. The highlighted code statements show the process.
Private Sub Command1_Click()
Dim xmldoc As DOMDocument30
Dim ProcInstr As IXMLDOMProcessingInstruction
Dim rootElement As IXMLDOMElement
Dim aElement As IXMLDOMElement
Dim cElement As IXMLDOMElement
Dim dElement As IXMLDOMElement
Dim att As IXMLDOMAttribute
'Creating DOM Document object
Set xmldoc = New DOMDocument30
'this adds the processing instruction
'the first line in an XML document
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")
'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
'create a child element, 'name' for the student
Set cElement = xmldoc.createElement("name")
'add a value for this node, 'Linda Jones'
cElement.nodeTypedValue = "Linda Jones"
'cElement is child of aElement
'add the cElement as a child of aElement
aElement.appendChild cElement
'append another child to the 'student' element
'-------------
Set dElement = xmldoc.createElement("legacySkill")
dElement.nodeTypedValue = "Access, VB5.0"
aElement.appendChild dElement
'-------attributes---
'---create an attribute using the createAttribute() method
'---at the same time set its name
Set att = xmldoc.createAttribute("id")
'---set the attributes 'text' property
att.Text = "1"
'--for the aElement, which is 'student' set a named
'---attribute, it's name is att
aElement.Attributes.setNamedItem att
'----------------------
'Saving the xml document to c:testWebStudents.xml
xmldoc.save "c:testWebStudents.xml"
WebBrowser1.Navigate2 ("c:testWebStudents.xml")
End Sub
When you execute the code by clicking the button, the form will be displayed as shown in the next picture. You may also notice that a second child to the student node has been added.

In writing the code you must leverage the intellisense support to the maximum extent if you want to avoid errors. Make use of the drop-down cue you get while coding as shown.

Next: Manipulating the XML document >>
More XML Articles
More By Jayaram Krishnaswamy