By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,133 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,133 IT Pros & Developers. It's quick & easy.

filling treeview from xml file, but only certain tags

P: n/a
I try to recursivley fill a treeview from a xml-file. It works well,
when the contents of the xml and the treeview match 1:1.

code here: http://mad-scientists.co.uk/micha/shownode.txt

a sample xml doc can be found here:

http://mad-scientists.co.uk/micha/CurrentSearch.xml

the problem is, that the xml file contains more items then I want to
show in the treeview.
I only want to show items with certain tags. (OPTEXT & ITEXT)
The other tags should be used to generate a SQL WHERE clause.
I tried various versions of

if tag = "this" then
nod=nodes.add(bla, child)
end if

but I simply don't get it to work. I'm desperate!
Jul 31 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
minimalniemand wrote:
I try to recursivley fill a treeview from a xml-file. It works well,
when the contents of the xml and the treeview match 1:1.

code here: http://mad-scientists.co.uk/micha/shownode.txt

a sample xml doc can be found here:

http://mad-scientists.co.uk/micha/CurrentSearch.xml

the problem is, that the xml file contains more items then I want to
show in the treeview.
I only want to show items with certain tags. (OPTEXT & ITEXT)
You have not even told us which programming language and which framework
and which tree view you are using. That sample looks like VB or VB.NET
although it is not clear why you would use MSXML with the .NET
framework. You might be better off asking in a group about classic VB if
that is what you use, or in a VB.NET group. Instead of processing all
child elements you could consider to use XPath to process only those
elements you are interested in to appear in the tree view.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 31 '08 #2

P: n/a
Thanks a lot for your answer.
I intentionally left the programming language out, cause an advice in
pseudo code should be ok for me.

but its VB6 & MSXML and I also posted in a VB group.

On 31 Jul., 14:57, Martin Honnen <mahotr...@yahoo.dewrote:
minimalniemand wrote:
I try to recursivley fill a treeview from a xml-file. It works well,
when the contents of the xml and the treeview match 1:1.
code here:http://mad-scientists.co.uk/micha/shownode.txt
a sample xml doc can be found here:
http://mad-scientists.co.uk/micha/CurrentSearch.xml
the problem is, that the xml file contains more items then I want to
show in the treeview.
I only want to show items with certain tags. (OPTEXT & ITEXT)

You have not even told us which programming language and which framework
and which tree view you are using. That sample looks like VB or VB.NET
although it is not clear why you would use MSXML with the .NET
framework. You might be better off asking in a group about classic VB if
that is what you use, or in a VB.NET group. Instead of processing all
child elements you could consider to use XPath to process only those
elements you are interested in to appear in the tree view.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 31 '08 #3

P: n/a
minimalniemand wrote:
but its VB6 & MSXML and I also posted in a VB group.
Use Msxml2.DOMDocument30 or later and then you can apply XPath to find
child nodes of the name e.g.
node.selectNodes("OPTEXT | ITEXT")
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 31 '08 #4

P: n/a
On 31 Jul., 15:15, Martin Honnen <mahotr...@yahoo.dewrote:
node.selectNodes("OPTEXT | ITEXT")
Will the hierarchy be kept in the resulting nodelist? If no, is there
any other possibility that keeps the hierarchy?


Jul 31 '08 #5

P: n/a
minimalniemand wrote:
On 31 Jul., 15:15, Martin Honnen <mahotr...@yahoo.dewrote:
> node.selectNodes("OPTEXT | ITEXT")

Will the hierarchy be kept in the resulting nodelist?
That expression returns a node list with child elements of name 'OPTEXT'
and 'ITEXT', you can then add them to your tree view for the current
level, then you can recursively process the next level. That way you
would get a tree with the hierarchy being preserved although you
obviously need to decide which nodes you add as none leaf nodes.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 31 '08 #6

P: n/a
again, thank you very much, your help is greatly appreciated!

I don't really understand what you mean by "although you obviously
need to decide which nodes you add as none leaf nodes."
The tree should look in the end like this (not conform with the
example in the starting post):

Clause(AND)
|__ Nationality=Belgian
|__ Gender=1
|__ Clause(OR)
|__ Studies=BE
|__ Studies=BI

which then should result in a SQL where clause like:

Nationality='Belorussian' AND Gender=1 AND (Studies='BE' OR
Studies='BI')

what would be a non leaf node in this case?



On 31 Jul., 16:40, Martin Honnen <mahotr...@yahoo.dewrote:
That expression returns a node list with child elements of name 'OPTEXT'
and 'ITEXT', you can then add them to your tree view for the current
level, then you can recursively process the next level. That way you
would get a tree with the hierarchy being preserved although you
obviously need to decide which nodes you add as none leaf nodes.

Aug 1 '08 #7

P: n/a
minimalniemand wrote:
I don't really understand what you mean by "although you obviously
need to decide which nodes you add as none leaf nodes."
The tree should look in the end like this (not conform with the
example in the starting post):

Clause(AND)
|__ Nationality=Belgian
|__ Gender=1
|__ Clause(OR)
|__ Studies=BE
|__ Studies=BI

which then should result in a SQL where clause like:

Nationality='Belorussian' AND Gender=1 AND (Studies='BE' OR
Studies='BI')

what would be a non leaf node in this case?
The leaf nodes are the conditions (e.g. Nationality=Belgian), the non
leaf nodes are the clauses (e.g. Clause(AND)).

Can you post the XML corresponding to that tree above? Then we should be
able to adapt the VB code you have.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 1 '08 #8

P: n/a
the XML file for the example can be found here: http://mad-scientists.co.uk/micha/example.xml

In the meantime, after reading a lot about XPath, XSL, XSLT ... (this
is my first project involving XML), I had another idea:

wouldn't it be possible to tranform the XMLdoc via XSL into another
XMLdoc, containing only the information I'd like to display and simply
load this with a normal recursive function (just thinking loudly...)?

however, I'll be happy with any solution =)

On 1 Aug., 13:47, Martin Honnen <mahotr...@yahoo.dewrote:
The leaf nodes are the conditions (e.g. Nationality=Belgian), the non
leaf nodes are the clauses (e.g. Clause(AND)).

Can you post the XML corresponding to that tree above? Then we should be
able to adapt the VB code you have.

Aug 1 '08 #9

P: n/a
minimalniemand wrote:
the XML file for the example can be found here: http://mad-scientists.co.uk/micha/example.xml
I don't program with classic VB so here is an example with VB.NET 2005:

Sub InitializeTree(ByVal treeView As TreeView, ByVal src As String)
Dim doc As New XmlDocument()
doc.Load(src)
AddNodes(treeView.Nodes, doc.DocumentElement.SelectNodes("*"))
End Sub

Sub AddNodes(ByVal treeNodes As TreeNodeCollection, ByVal xmlNodes
As XmlNodeList)
For Each childElem As XmlNode In xmlNodes
Select Case childElem.LocalName
Case "CLAUSE"
Dim treeNode As New
TreeNode(childElem.SelectSingleNode("OPTEXT").Inne rText)
treeNodes.Add(treeNode)
AddNodes(treeNode.Nodes,
childElem.SelectNodes("ITEMS/*"))
Case "ITEM"
treeNodes.Add(New
TreeNode(childElem.SelectSingleNode("ITEXT").Inner Text))
End Select
Next

End Sub

It is then called as
InitializeTree(TreeView1, "..\..\XMLFile1.xml")
where TreeView1 is the tree view you want add nodes to and the second
argument is a string with the path to the XML file.

I realize you are not using .NET but classic VB and MSXML so here are
some hints how to translate the .NET XML DOM properties and methods to
MSXML and classic VB:
1) use selectNodes instead of SelectNodes
2) use selectSingleNode instead of SelectSingleNode
3) use text instead of InnerText
4) use MSXML2.DOMDocument30 instead of XmlDocument
5) make sure you call
xmlDocument.setProperty "SelectionLanguage", "XPath"
on your MSXML DOM document before you use selectNodes/selectSingleNode
--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 1 '08 #10

P: n/a
I had to rewrite it this way:

Sub InitializeTree(ByVal src As String)

xml.Load (src)
xml.setProperty "SelectionLanguage", "XPath"
Call AddNodes(Me!ocxTreeView.Nodes,
xml.documentElement.selectNodes("*"))

End Sub

Sub AddNodes(ByVal treeNodes As Nodes, ByVal xmlNodes As
MSXML2.IXMLDOMNodeList)

For Each x1 In xmlNodes
Select Case x1.tagName
Case "CLAUSE"
Dim treeNode As Node
Set treeNode = treeNodes.Add(, , ,
x1.selectSingleNode("OPTEXT").Text)
treeNodes.Add (treeNode)
Call AddNodes(treeNode.Nodes, x1.selectNodes("ITEMS/*"))
Case "ITEM"
treeNodes.Add
(treeNodes.Add(x1.selectSingleNode("ITEXT").Text))
End Select
Next
End Sub

But its not yet working. The Nodes Property of treeNode does not exist
in VB, what exactly is it? Is it a list of all children under
treeNode? there aint such a collection I'm afraid
On 1 Aug., 16:17, Martin Honnen <mahotr...@yahoo.dewrote:
minimalniemand wrote:
the XML file for the example can be found here:http://mad-scientists.co.uk/micha/example.xml

I don't program with classic VB so here is an example with VB.NET 2005:

Sub InitializeTree(ByVal treeView As TreeView, ByVal src As String)
Dim doc As New XmlDocument()
doc.Load(src)
AddNodes(treeView.Nodes, doc.DocumentElement.SelectNodes("*"))
End Sub

Sub AddNodes(ByVal treeNodes As TreeNodeCollection, ByVal xmlNodes
As XmlNodeList)
For Each childElem As XmlNode In xmlNodes
Select Case childElem.LocalName
Case "CLAUSE"
Dim treeNode As New
TreeNode(childElem.SelectSingleNode("OPTEXT").Inne rText)
treeNodes.Add(treeNode)
AddNodes(treeNode.Nodes,
childElem.SelectNodes("ITEMS/*"))
Case "ITEM"
treeNodes.Add(New
TreeNode(childElem.SelectSingleNode("ITEXT").Inner Text))

End Select
Next

End Sub

It is then called as
InitializeTree(TreeView1, "..\..\XMLFile1.xml")
where TreeView1 is the tree view you want add nodes to and the second
argument is a string with the path to the XML file.

I realize you are not using .NET but classic VB and MSXML so here are
some hints how to translate the .NET XML DOM properties and methods to
MSXML and classic VB:
1) use selectNodes instead of SelectNodes
2) use selectSingleNode instead of SelectSingleNode
3) use text instead of InnerText
4) use MSXML2.DOMDocument30 instead of XmlDocument
5) make sure you call
xmlDocument.setProperty "SelectionLanguage", "XPath"
on your MSXML DOM document before you use selectNodes/selectSingleNode

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 1 '08 #11

P: n/a
minimalniemand wrote:
But its not yet working. The Nodes Property of treeNode does not exist
in VB, what exactly is it? Is it a list of all children under
treeNode? there aint such a collection I'm afraid
Yes, the Nodes property is the collection of child nodes of a tree view
node. It is passed to the sub to be able to add the newly created nodes.
If there is no such collection in your tree view API then you need to
pass in a node you want to add children to. Only that way the initial
call needs to be changed as you have no node to pass in at that moment.
I am afraid I can't provide more help, I am not familiar with the API
your tree view control has. In terms of processing the XML your code
should work, you will just have to find a way to create the
corresponding tree view nodes to cater for the API of your tree view
control.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 1 '08 #12

P: n/a
I havent really figured out how yet, but it works. It works great!

Thank you so much!

On 1 Aug., 18:38, Martin Honnen <mahotr...@yahoo.dewrote:
minimalniemand wrote:
But its not yet working. The Nodes Property of treeNode does not exist
in VB, what exactly is it? Is it a list of all children under
treeNode? there aint such a collection I'm afraid

Yes, the Nodes property is the collection of child nodes of a tree view
node. It is passed to the sub to be able to add the newly created nodes.
If there is no such collection in your tree view API then you need to
pass in a node you want to add children to. Only that way the initial
call needs to be changed as you have no node to pass in at that moment.
I am afraid I can't provide more help, I am not familiar with the API
your tree view control has. In terms of processing the XML your code
should work, you will just have to find a way to create the
corresponding tree view nodes to cater for the API of your tree view
control.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Aug 5 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.