Discussion:
Accessing a node attribute XML VB6 Visual Basic 6
(too old to reply)
Wilburt Leftlong
2004-05-25 18:42:29 UTC
Permalink
I'm adding an XML node values to a tree like so:
Private Sub bttnLoad_Click()
On Error GoTo myErr
Dim xmlDoc As DOMDocument30
Set xmlDoc = New DOMDocument30
xmlDoc.async = False
If Not xmlDoc.Load("C:\XMLNodes.xml") Then
MsgBox "Could not load file"
Exit Sub
End If
TreeView1.Nodes.Clear
Dim iNode As Integer
Dim newElement As IXMLDOMElement
For iNode = 0 To xmlDoc.getElementsByTagName("NODE").length - 1
Set newElement = xmlDoc.getElementsByTagName("NODE").Item(iNode)
'if no parent key, it must be a root element
If newElement.getAttribute("ParentKey") = "" Then
TreeView1.Nodes.Add , , newElement.getAttribute("Key"),
newElement.getAttribute("Tag")

Else
TreeView1.Nodes.Add newElement.getAttribute("ParentKey"),
tvwChild, newElement.getAttribute("Key"), newElement.getAttribute("Caption")
End If

Next
myErr:
If Err.Number > 0 Then
MsgBox Err.Description
End If
End Sub
With an XML file that looks like this:

<NODES>
<NODE Caption="fdf445df6" Key="K7055473" Tag="sdfds" ParentKey=""/>
<NODE Caption="sfdf3222" Key="K7055474" Tag="sdsdf" ParentKey=""/>
<NODE Caption="ffd3434" Key="K7055475" Tag="sfds" ParentKey="K7055474"/>
<NODE Caption="asdasdasd" Key="K7055476" Tag="sdds" ParentKey="K7055473"/>
</NODES>
And attempting to display the current node in a textbox like so:


Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
nodeValue.Text = TreeView1.SelectedItem.Tag
End Sub




I am unable to access the value of attribute "Tag" for some reason though.
I can however dislay the value of "Key", and others.


Am I loading my XML file into my tree incorrectly?

Thanks

















---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 5/22/2004
Douglas Marquardt
2004-05-25 20:00:32 UTC
Permalink
Hi Wilburt:

I don't see you assinging a value to the node.Tag property?
You are assigning the xml tag value to the node.Text property;
did you want to assign that to the node.Tag as well?
If so, then you would need to do something like this:

Dim nod As Node
...etc
Set nod =TreeView1.Nodes.Add (, , newElement.getAttribute("Key"),
newElement.getAttribute("Tag"))
nod.Tag = newElement.getAttribute("Tag")

Hope this helps,

Doug.
Post by Wilburt Leftlong
Private Sub bttnLoad_Click()
On Error GoTo myErr
Dim xmlDoc As DOMDocument30
Set xmlDoc = New DOMDocument30
xmlDoc.async = False
If Not xmlDoc.Load("C:\XMLNodes.xml") Then
MsgBox "Could not load file"
Exit Sub
End If
TreeView1.Nodes.Clear
Dim iNode As Integer
Dim newElement As IXMLDOMElement
For iNode = 0 To xmlDoc.getElementsByTagName("NODE").length - 1
Set newElement = xmlDoc.getElementsByTagName("NODE").Item(iNode)
'if no parent key, it must be a root element
If newElement.getAttribute("ParentKey") = "" Then
TreeView1.Nodes.Add , , newElement.getAttribute("Key"),
newElement.getAttribute("Tag")
Else
TreeView1.Nodes.Add newElement.getAttribute("ParentKey"),
tvwChild, newElement.getAttribute("Key"), newElement.getAttribute("Caption")
End If
Next
If Err.Number > 0 Then
MsgBox Err.Description
End If
End Sub
<NODES>
<NODE Caption="fdf445df6" Key="K7055473" Tag="sdfds" ParentKey=""/>
<NODE Caption="sfdf3222" Key="K7055474" Tag="sdsdf" ParentKey=""/>
<NODE Caption="ffd3434" Key="K7055475" Tag="sfds" ParentKey="K7055474"/>
<NODE Caption="asdasdasd" Key="K7055476" Tag="sdds" ParentKey="K7055473"/>
</NODES>
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
nodeValue.Text = TreeView1.SelectedItem.Tag
End Sub
I am unable to access the value of attribute "Tag" for some reason though.
I can however dislay the value of "Key", and others.
Am I loading my XML file into my tree incorrectly?
Thanks
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 5/22/2004
Wilburt Leftlong
2004-05-25 21:14:18 UTC
Permalink
Hi Doug. Thanks again.

Do I have to assign each element for a single node, or can I just assign
all elements by default?
How can I assign multiple elements to a single node using:

TreeView1.Nodes.Add

?

If I try and add as you suggested I get an "key is not unique" error. Is
there a way to add multiple values per "add"?


This whole thing is really confusing me. I apologize.
Post by Douglas Marquardt
I don't see you assinging a value to the node.Tag property?
You are assigning the xml tag value to the node.Text property;
did you want to assign that to the node.Tag as well?
Dim nod As Node
...etc
Set nod =TreeView1.Nodes.Add (, , newElement.getAttribute("Key"),
newElement.getAttribute("Tag"))
nod.Tag = newElement.getAttribute("Tag")
Hope this helps,
Doug.
Post by Wilburt Leftlong
Private Sub bttnLoad_Click()
On Error GoTo myErr
Dim xmlDoc As DOMDocument30
Set xmlDoc = New DOMDocument30
xmlDoc.async = False
If Not xmlDoc.Load("C:\XMLNodes.xml") Then
MsgBox "Could not load file"
Exit Sub
End If
TreeView1.Nodes.Clear
Dim iNode As Integer
Dim newElement As IXMLDOMElement
For iNode = 0 To xmlDoc.getElementsByTagName("NODE").length - 1
Set newElement = xmlDoc.getElementsByTagName("NODE").Item(iNode)
'if no parent key, it must be a root element
If newElement.getAttribute("ParentKey") = "" Then
TreeView1.Nodes.Add , , newElement.getAttribute("Key"),
newElement.getAttribute("Tag")
Else
TreeView1.Nodes.Add
newElement.getAttribute("ParentKey"),
Post by Douglas Marquardt
Post by Wilburt Leftlong
tvwChild, newElement.getAttribute("Key"),
newElement.getAttribute("Caption")
Post by Douglas Marquardt
Post by Wilburt Leftlong
End If
Next
If Err.Number > 0 Then
MsgBox Err.Description
End If
End Sub
<NODES>
<NODE Caption="fdf445df6" Key="K7055473" Tag="sdfds" ParentKey=""/>
<NODE Caption="sfdf3222" Key="K7055474" Tag="sdsdf" ParentKey=""/>
<NODE Caption="ffd3434" Key="K7055475" Tag="sfds" ParentKey="K7055474"/>
<NODE Caption="asdasdasd" Key="K7055476" Tag="sdds"
ParentKey="K7055473"/>
Post by Douglas Marquardt
Post by Wilburt Leftlong
</NODES>
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
nodeValue.Text = TreeView1.SelectedItem.Tag
End Sub
I am unable to access the value of attribute "Tag" for some reason though.
I can however dislay the value of "Key", and others.
Am I loading my XML file into my tree incorrectly?
Thanks
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 5/22/2004
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 5/22/2004
Douglas Marquardt
2004-05-25 22:10:27 UTC
Permalink
Hi Wilburt:

I don't understand what you mean by "assign multiple elements to a single node".
Does not each element (Node) in your xml represent a node in the treeview?
If so, then you would have to build your tree node by node.

wrt the error, the example I posted did not change any keys you were
assigning so I don't see how it could have caused any new errors; all it
did was update the Tag property of the new node you added.

In any case, here is the code I used to load the treeview with the xml you posted:

Dim xmlDoc As DOMDocument30
Dim nod As Node
Dim iNode As Integer
Dim newElement As IXMLDOMElement

On Error GoTo myErr

Set xmlDoc = New DOMDocument30
xmlDoc.async = False
If Not xmlDoc.Load("C:\XMLNodes.xml") Then
MsgBox "Could not load file"
Exit Sub
End If

TreeView1.Nodes.Clear
For iNode = 0 To xmlDoc.getElementsByTagName("NODE").length - 1
Set newElement = xmlDoc.getElementsByTagName("NODE").Item(iNode)

'if no parent key, it must be a root element
If newElement.getAttribute("ParentKey") = "" Then
Set nod = TreeView1.Nodes.Add(, , newElement.getAttribute("Key"), newElement.getAttribute("Caption"))
Else
Set nod = TreeView1.Nodes.Add(newElement.getAttribute("ParentKey"), tvwChild, newElement.getAttribute("Key"),
newElement.getAttribute("Caption"))
End If
nod.Tag = newElement.getAttribute("Tag")
Next
myErr:
If Err.Number > 0 Then
MsgBox Err.Description
End If


Hope this helps,

Doug.
Post by Wilburt Leftlong
Hi Doug. Thanks again.
Do I have to assign each element for a single node, or can I just assign
all elements by default?
TreeView1.Nodes.Add
?
If I try and add as you suggested I get an "key is not unique" error. Is
there a way to add multiple values per "add"?
This whole thing is really confusing me. I apologize.
Post by Douglas Marquardt
I don't see you assinging a value to the node.Tag property?
You are assigning the xml tag value to the node.Text property;
did you want to assign that to the node.Tag as well?
Dim nod As Node
...etc
Set nod =TreeView1.Nodes.Add (, , newElement.getAttribute("Key"),
newElement.getAttribute("Tag"))
nod.Tag = newElement.getAttribute("Tag")
Hope this helps,
Doug.
Post by Wilburt Leftlong
Private Sub bttnLoad_Click()
On Error GoTo myErr
Dim xmlDoc As DOMDocument30
Set xmlDoc = New DOMDocument30
xmlDoc.async = False
If Not xmlDoc.Load("C:\XMLNodes.xml") Then
MsgBox "Could not load file"
Exit Sub
End If
TreeView1.Nodes.Clear
Dim iNode As Integer
Dim newElement As IXMLDOMElement
For iNode = 0 To xmlDoc.getElementsByTagName("NODE").length - 1
Set newElement = xmlDoc.getElementsByTagName("NODE").Item(iNode)
'if no parent key, it must be a root element
If newElement.getAttribute("ParentKey") = "" Then
TreeView1.Nodes.Add , , newElement.getAttribute("Key"),
newElement.getAttribute("Tag")
Else
TreeView1.Nodes.Add
newElement.getAttribute("ParentKey"),
Post by Douglas Marquardt
Post by Wilburt Leftlong
tvwChild, newElement.getAttribute("Key"),
newElement.getAttribute("Caption")
Post by Douglas Marquardt
Post by Wilburt Leftlong
End If
Next
If Err.Number > 0 Then
MsgBox Err.Description
End If
End Sub
<NODES>
<NODE Caption="fdf445df6" Key="K7055473" Tag="sdfds" ParentKey=""/>
<NODE Caption="sfdf3222" Key="K7055474" Tag="sdsdf" ParentKey=""/>
<NODE Caption="ffd3434" Key="K7055475" Tag="sfds" ParentKey="K7055474"/>
<NODE Caption="asdasdasd" Key="K7055476" Tag="sdds"
ParentKey="K7055473"/>
Post by Douglas Marquardt
Post by Wilburt Leftlong
</NODES>
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
nodeValue.Text = TreeView1.SelectedItem.Tag
End Sub
I am unable to access the value of attribute "Tag" for some reason
though.
Post by Douglas Marquardt
Post by Wilburt Leftlong
I can however dislay the value of "Key", and others.
Am I loading my XML file into my tree incorrectly?
Thanks
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 5/22/2004
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 5/22/2004
Wilburt Leftlong
2004-05-26 15:39:36 UTC
Permalink
Thank you. That helped.
Post by Douglas Marquardt
I don't understand what you mean by "assign multiple elements to a single node".
Does not each element (Node) in your xml represent a node in the treeview?
If so, then you would have to build your tree node by node.
wrt the error, the example I posted did not change any keys you were
assigning so I don't see how it could have caused any new errors; all it
did was update the Tag property of the new node you added.
Dim xmlDoc As DOMDocument30
Dim nod As Node
Dim iNode As Integer
Dim newElement As IXMLDOMElement
On Error GoTo myErr
Set xmlDoc = New DOMDocument30
xmlDoc.async = False
If Not xmlDoc.Load("C:\XMLNodes.xml") Then
MsgBox "Could not load file"
Exit Sub
End If
TreeView1.Nodes.Clear
For iNode = 0 To xmlDoc.getElementsByTagName("NODE").length - 1
Set newElement = xmlDoc.getElementsByTagName("NODE").Item(iNode)
'if no parent key, it must be a root element
If newElement.getAttribute("ParentKey") = "" Then
Set nod = TreeView1.Nodes.Add(, , newElement.getAttribute("Key"),
newElement.getAttribute("Caption"))
Post by Douglas Marquardt
Else
Set nod =
TreeView1.Nodes.Add(newElement.getAttribute("ParentKey"), tvwChild,
newElement.getAttribute("Key"),
Post by Douglas Marquardt
newElement.getAttribute("Caption"))
End If
nod.Tag = newElement.getAttribute("Tag")
Next
If Err.Number > 0 Then
MsgBox Err.Description
End If
Hope this helps,
Doug.
Post by Wilburt Leftlong
Hi Doug. Thanks again.
Do I have to assign each element for a single node, or can I just assign
all elements by default?
TreeView1.Nodes.Add
?
If I try and add as you suggested I get an "key is not unique" error. Is
there a way to add multiple values per "add"?
This whole thing is really confusing me. I apologize.
Post by Douglas Marquardt
I don't see you assinging a value to the node.Tag property?
You are assigning the xml tag value to the node.Text property;
did you want to assign that to the node.Tag as well?
Dim nod As Node
...etc
Set nod =TreeView1.Nodes.Add (, , newElement.getAttribute("Key"),
newElement.getAttribute("Tag"))
nod.Tag = newElement.getAttribute("Tag")
Hope this helps,
Doug.
Post by Wilburt Leftlong
Private Sub bttnLoad_Click()
On Error GoTo myErr
Dim xmlDoc As DOMDocument30
Set xmlDoc = New DOMDocument30
xmlDoc.async = False
If Not xmlDoc.Load("C:\XMLNodes.xml") Then
MsgBox "Could not load file"
Exit Sub
End If
TreeView1.Nodes.Clear
Dim iNode As Integer
Dim newElement As IXMLDOMElement
For iNode = 0 To xmlDoc.getElementsByTagName("NODE").length - 1
Set newElement =
xmlDoc.getElementsByTagName("NODE").Item(iNode)
Post by Douglas Marquardt
Post by Wilburt Leftlong
Post by Douglas Marquardt
Post by Wilburt Leftlong
'if no parent key, it must be a root element
If newElement.getAttribute("ParentKey") = "" Then
TreeView1.Nodes.Add , ,
newElement.getAttribute("Key"),
Post by Douglas Marquardt
Post by Wilburt Leftlong
Post by Douglas Marquardt
Post by Wilburt Leftlong
newElement.getAttribute("Tag")
Else
TreeView1.Nodes.Add
newElement.getAttribute("ParentKey"),
Post by Douglas Marquardt
Post by Wilburt Leftlong
tvwChild, newElement.getAttribute("Key"),
newElement.getAttribute("Caption")
Post by Douglas Marquardt
Post by Wilburt Leftlong
End If
Next
If Err.Number > 0 Then
MsgBox Err.Description
End If
End Sub
<NODES>
<NODE Caption="fdf445df6" Key="K7055473" Tag="sdfds" ParentKey=""/>
<NODE Caption="sfdf3222" Key="K7055474" Tag="sdsdf" ParentKey=""/>
<NODE Caption="ffd3434" Key="K7055475" Tag="sfds"
ParentKey="K7055474"/>
Post by Douglas Marquardt
Post by Wilburt Leftlong
Post by Douglas Marquardt
Post by Wilburt Leftlong
<NODE Caption="asdasdasd" Key="K7055476" Tag="sdds"
ParentKey="K7055473"/>
Post by Douglas Marquardt
Post by Wilburt Leftlong
</NODES>
Private Sub TreeView1_NodeClick(ByVal Node As MSComctlLib.Node)
nodeValue.Text = TreeView1.SelectedItem.Tag
End Sub
I am unable to access the value of attribute "Tag" for some reason
though.
Post by Douglas Marquardt
Post by Wilburt Leftlong
I can however dislay the value of "Key", and others.
Am I loading my XML file into my tree incorrectly?
Thanks
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 5/22/2004
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 5/22/2004
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.690 / Virus Database: 451 - Release Date: 5/23/2004

Loading...