VB6 & MSXML4: Issue Adding Attributes
I'm having trouble populating attributes in the code below. It works for the root but not the child level. I've tried various ways but can't get it to work. Please help me if you can. Thanks!
Private Sub cmdTestVBF_Click()
Dim oRoot As IXMLDOMElement
Dim oLevel1 As IXMLDOMElement
Dim oLevel2 As IXMLDOMElement
Dim oNode As IXMLDOMNode
XMLOUTPATH = App.Path & "\XmlOuput_TestVBF.xml"
Set oDoc = New DOMDocument
oDoc.resolveExternals = True
Set oRoot = oDoc.createElement("Parent")
Set oDoc.documentElement = oRoot
oRoot.setAttribute "xmlns", "http://blah/blah/blah"
oRoot.setAttribute "xmlns:i", "http://www.w3.org/2001/XMLSchema-instance"
Set oNode = oDoc.createElement("Child1")
oNode.Text = "2010-03-08T00:00:00"
oRoot.appendChild oNode
Set oLevel1 = oDoc.createElement("Child2")
oRoot.appendChild oLevel1
Set oLevel2 = oDoc.createElement("Grandchild")
'Want entry to look like: <Grandchild i:type="abc">
oLevel1.appendChild oLevel2
Set oNode = oDoc.createElement("GreatGrandchild")
oNode.Text = "Adding text works fine"
oLevel2.appendChild oNode
Set oNode = oDoc.createElement("Child3")
oNode.Text = "Adding text works fine"
oRoot.appendChild oNode
Set oNode = oDoc.createElement("Child4")
'Want entry to look like: <Child4 i:nil="true" />
oRoot.appendChild oNode
' Save xml file
oDoc.save XMLOUTPATH
MsgBox XMLOUTPATH & " is created for you."
End Sub
DESIRED OUTPUT (spacing not shown correctly in this post but code generates properly spaced XML file. I just need help on how to populate attributes.)
- <Parent xmlns="http://blah/blah/blah" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Child1>2010-03-08T00:00:00</Child1>
- <Child2>
- <Grandchild i:type="abc">
<GreatGrandchild>Adding text OK</GreatGrandchild>
</Grandchild>
</Child2>
<Child3>Adding text OK</Child3>
<Child4 i:nil="true" />
</Parent>