0 Replies Latest reply on Dec 3, 2009 2:01 AM by xandesign

    Need help finding node in xml by attribute value?

    xandesign

      Hi,

       

      I'm trying to write a class that builds an xml object that I can use to populate flex 3 tree controls in my application. I have added the class code below so that you can see what I have so far. I need to be able to add, remove and edit elements from my xml object.

       

      When trying to add elements to the xml I pass the parent id (maps to a category id) the label and id of the new element (document or category), I then want to use the parent id to search the xml object for a matching category. I then will need to count how many childrent the matching category has so that I can add the new element to the end of the matching categories child elements. Also, I need to list categories before documents under each parent category.

       

      I hope that all makes sense. Anyway, if any one can help I would be very grateful.

       

      Thanks in advance,

       

      Xander

       

      I want the getTree method to return an XMLList object like so

      <category id="1" label="node1"/>
      <category id="2" label="node2">
          <category id="5" label="node2.1">
              <document id="3">title 3</document>
          </category>
          <document id="1">title 1</document>
          <document id="2">title 2</document>
      </category>
      <category id="3" label="node3">
          <document id="4">title 4</document>
      </category>
      <category id="4" label="node4"/>

       

      but so far I can only get my class to return

      <category id="1" label="node1"/>
      <category id="2" label="node2"/>
      <category id="3" label="node3"/>
      <category id="4" label="node4"/>
      <category id="5" label="node2.1"/>
      

       

      This is my class code

      package net.myclasses.xml {
           
           public class XMLTree {
                private var root:XML;
                
                public function XMLTree() {
                     root = <dataset></dataset>;
                }
                
                public function addCategory(pid:int, id:int, title:String):void {
                     var leaf:XML = <category/>;
                     leaf.@id = id;
                     leaf.@label = title;
                     
                     var node:XML = findNodeById(pid);
                     if (node == null) {
                          root.appendChild(leaf);
                     } else {
                          node.appendChild(leaf);
                     }
                }
                
                public function addDocument(pid:int, id:int, title:String):void {
                     var leaf:XML = <document/>;
                     leaf.@id = id;
                     leaf.@label = title;
                     
                     var node:XML = findNodeById(pid);
                     node.appendChild(leaf);
                }
                
                public function getTree():XMLList {
                     return root.children();
                }
                
                private function findNodeById(id:int):XML {
                     return root.elements("category").(@id==id) as XML;
                }
           }
      }
      

       

      and this is how I call the class object from my application

      var xmltree:XMLTree = new XMLTree();
      
      xmltree.addCategory(0, 1, "node1");
      xmltree.addCategory(0, 2, "node2");
      xmltree.addCategory(0, 3, "node3");
      xmltree.addCategory(2, 4, "node2.1");
      
      mytree.dataProvider = xmltree.getTree();