filling treeview from xml file, but only certain tags

M

minimalniemand

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!
 
M

Martin Honnen

minimalniemand said:
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.
 
M

minimalniemand

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.
 
M

Martin Honnen

minimalniemand said:
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")
 
M

minimalniemand

node.selectNodes("OPTEXT | ITEXT")

Will the hierarchy be kept in the resulting nodelist? If no, is there
any other possibility that keeps the hierarchy?
 
M

Martin Honnen

minimalniemand said:
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.
 
M

minimalniemand

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?
 
M

Martin Honnen

minimalniemand said:
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.
 
M

minimalniemand

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 =)
 
M

Martin Honnen

minimalniemand said:
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").InnerText)
treeNodes.Add(treeNode)
AddNodes(treeNode.Nodes,
childElem.SelectNodes("ITEMS/*"))
Case "ITEM"
treeNodes.Add(New
TreeNode(childElem.SelectSingleNode("ITEXT").InnerText))


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
 
M

minimalniemand

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
 
M

Martin Honnen

minimalniemand said:
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.
 
M

minimalniemand

I havent really figured out how yet, but it works. It works great!

Thank you so much!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top