filling treeview from xml file, but only certain tags

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • minimalniemand

    filling treeview from xml file, but only certain tags

    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:



    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(b la, child)
    end if

    but I simply don't get it to work. I'm desperate!
  • Martin Honnen

    #2
    Re: filling treeview from xml file, but only certain tags

    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:
    >

    >
    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

    Comment

    • minimalniemand

      #3
      Re: filling treeview from xml file, but only certain tags

      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...@yaho o.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.
      >>
      a sample xml doc can be found here:
      >>
      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/

      Comment

      • Martin Honnen

        #4
        Re: filling treeview from xml file, but only certain tags

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

        Martin Honnen

        Comment

        • minimalniemand

          #5
          Re: filling treeview from xml file, but only certain tags

          On 31 Jul., 15:15, Martin Honnen <mahotr...@yaho o.dewrote:
          node.selectNode s("OPTEXT | ITEXT")
          Will the hierarchy be kept in the resulting nodelist? If no, is there
          any other possibility that keeps the hierarchy?






          Comment

          • Martin Honnen

            #6
            Re: filling treeview from xml file, but only certain tags

            minimalniemand wrote:
            On 31 Jul., 15:15, Martin Honnen <mahotr...@yaho o.dewrote:
            > node.selectNode s("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

            Comment

            • minimalniemand

              #7
              Re: filling treeview from xml file, but only certain tags

              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=Bel gian
              |__ Gender=1
              |__ Clause(OR)
              |__ Studies=BE
              |__ Studies=BI

              which then should result in a SQL where clause like:

              Nationality='Be lorussian' 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...@yaho o.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.



              Comment

              • Martin Honnen

                #8
                Re: filling treeview from xml file, but only certain tags

                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=Bel gian
                |__ Gender=1
                |__ Clause(OR)
                |__ Studies=BE
                |__ Studies=BI
                >
                which then should result in a SQL where clause like:
                >
                Nationality='Be lorussian' 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=Bel gian), 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

                Comment

                • minimalniemand

                  #9
                  Re: filling treeview from xml file, but only certain tags

                  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...@yaho o.dewrote:
                  The leaf nodes are the conditions (e.g. Nationality=Bel gian), 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.
                  >

                  Comment

                  • Martin Honnen

                    #10
                    Re: filling treeview from xml file, but only certain tags

                    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(treeVi ew.Nodes, doc.DocumentEle ment.SelectNode s("*"))
                    End Sub

                    Sub AddNodes(ByVal treeNodes As TreeNodeCollect ion, ByVal xmlNodes
                    As XmlNodeList)
                    For Each childElem As XmlNode In xmlNodes
                    Select Case childElem.Local Name
                    Case "CLAUSE"
                    Dim treeNode As New
                    TreeNode(childE lem.SelectSingl eNode("OPTEXT") .InnerText)
                    treeNodes.Add(t reeNode)
                    AddNodes(treeNo de.Nodes,
                    childElem.Selec tNodes("ITEMS/*"))
                    Case "ITEM"
                    treeNodes.Add(N ew
                    TreeNode(childE lem.SelectSingl eNode("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 selectSingleNod e instead of SelectSingleNod e
                    3) use text instead of InnerText
                    4) use MSXML2.DOMDocum ent30 instead of XmlDocument
                    5) make sure you call
                    xmlDocument.set Property "SelectionLangu age", "XPath"
                    on your MSXML DOM document before you use selectNodes/selectSingleNod e


                    --

                    Martin Honnen

                    Comment

                    • minimalniemand

                      #11
                      Re: filling treeview from xml file, but only certain tags

                      I had to rewrite it this way:

                      Sub InitializeTree( ByVal src As String)

                      xml.Load (src)
                      xml.setProperty "SelectionLangu age", "XPath"
                      Call AddNodes(Me!ocx TreeView.Nodes,
                      xml.documentEle ment.selectNode s("*"))

                      End Sub

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

                      For Each x1 In xmlNodes
                      Select Case x1.tagName
                      Case "CLAUSE"
                      Dim treeNode As Node
                      Set treeNode = treeNodes.Add(, , ,
                      x1.selectSingle Node("OPTEXT"). Text)
                      treeNodes.Add (treeNode)
                      Call AddNodes(treeNo de.Nodes, x1.selectNodes( "ITEMS/*"))
                      Case "ITEM"
                      treeNodes.Add
                      (treeNodes.Add( x1.selectSingle Node("ITEXT").T ext))
                      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...@yaho o.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(treeVi ew.Nodes, doc.DocumentEle ment.SelectNode s("*"))
                      End Sub
                      >
                      Sub AddNodes(ByVal treeNodes As TreeNodeCollect ion, ByVal xmlNodes
                      As XmlNodeList)
                      For Each childElem As XmlNode In xmlNodes
                      Select Case childElem.Local Name
                      Case "CLAUSE"
                      Dim treeNode As New
                      TreeNode(childE lem.SelectSingl eNode("OPTEXT") .InnerText)
                      treeNodes.Add(t reeNode)
                      AddNodes(treeNo de.Nodes,
                      childElem.Selec tNodes("ITEMS/*"))
                      Case "ITEM"
                      treeNodes.Add(N ew
                      TreeNode(childE lem.SelectSingl eNode("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 selectSingleNod e instead of SelectSingleNod e
                      3) use text instead of InnerText
                      4) use MSXML2.DOMDocum ent30 instead of XmlDocument
                      5) make sure you call
                      xmlDocument.set Property "SelectionLangu age", "XPath"
                      on your MSXML DOM document before you use selectNodes/selectSingleNod e
                      >
                      --
                      >
                      Martin Honnen
                      http://JavaScript.FAQTs.com/

                      Comment

                      • Martin Honnen

                        #12
                        Re: filling treeview from xml file, but only certain tags

                        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

                        Comment

                        • minimalniemand

                          #13
                          Re: filling treeview from xml file, but only certain tags

                          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...@yaho o.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/

                          Comment

                          Working...