5 Replies Latest reply on Dec 9, 2009 4:18 AM by CobraFlow

    How do I check flex xml object for children and return result as xmllist

    xandesign

      Hi all,

       

      I have a flex 3 xml object which when the object is created has no children, (defined below).

       

       var root:XML = <dataset/>;
       root.@id = 0;
      

       

      What I'm looking to do is create a little search function which searches the whole xml object for the existense of an id attribute value and if found return an XMLList object which I can add new nodes to or if not found return null. I've added the code I have so far, although it doesn't work. At the moment the code just returns an error which states I can't use appendChild on an empty object.

       

      This is how I call the addNode function:
      ...
      xmltree.addNode(0, 1, "node1");
      xmltree.addNode(1, 2, "node2");
      ...
      
      
      public function addNode(pid:int, id:int, title:String):void {
          var leaf:XML = <category/>;
          leaf.@id = id;
          leaf.@label = title;
      
          var node:XMLList = getNodeByPid(pid);
          if (node == null) {
              root.appendChild(leaf);
          } else {
              node.appendChild(leaf);
          }
      }
      
      private function getNodeByPid(pid:int):XMLList {
          var xmllist:XMLList = root.element("category").(@id=pid);
      
          return xmllist;
      }
      

       

      Any help would be much appreciated, thanks in advance,

       

      Xander

        • 1. Re: How do I check flex xml object for children and return result as xmllist
          rgadiparthi Level 2

          try to create your root xml like

          var root:XML = <dataset format="e4x"> </dataset>

           


          Thanks,

          Ranga G

          • 2. Re: How do I check flex xml object for children and return result as xmllist
            xandesign Level 1

            Thanks but how do I check the root for child nodes without getting an error message when the root has no child nodes?

            • 3. Re: How do I check flex xml object for children and return result as xmllist
              CobraFlow

              Firstly check that the node has children :-

              if(root.children().length() > 0)

                   root.appendChild(leaf);

              else

                   root.setChildren(leaf);

               

              You need to do setChildren() if the node is empty.

               

               

              Lewis

              • 4. Re: How do I check flex xml object for children and return result as xmllist
                xandesign Level 1

                I tried that but still getting the following error

                The appendChild method only works on lists containing one item

                 

                Here is my code with your suggestions added

                Still called in the same way as before
                var xmltree:XMLTree = new XMLTree();
                xmltree.addNode(0, 1, "node1");
                xmltree.addNode(0, 2, "node2");
                xmltree.addNode(1, 3, "node3");
                
                public function XMLTree() {
                     root = <dataset/>;
                     root.@format = "e4x";
                     root.@id = 0;
                }
                
                public function addNode(pid:int, id:int, title:String):void {
                     var leaf:XML = <category/>;
                     leaf.@id = id;
                     leaf.@label = title;
                
                     var node:XMLList = getNodeByPid(pid);
                     if (node == null) {
                          root.setChildren(leaf);
                     } else {
                          node.appendChild(leaf);
                     }
                }
                
                private function getNodeByPid(pid:int):XMLList {
                     var x:XMLList = null;
                
                     if (root.children().length()>0) {
                          x = root.elements("category").(@id==pid);
                     }
                
                     return x;
                }
                

                 

                Any ideas why I'm getting this error?

                 

                Xander

                • 5. Re: How do I check flex xml object for children and return result as xmllist
                  CobraFlow Level 1

                      public function XMLTree() {
                           root = <dataset/>;
                           root.@format = "e4x";

                       // The root id is not used here...
                           root.@id = 0;
                      }

                   

                      public function addNode(pid:int, id:int, title:String):void {
                          var leaf:XML = <category/>;
                          leaf.@id = id;
                          leaf.@label = title;

                   

                          var node:XML = getNodeByPid(pid);

                          // If we don't find the parent, default to the root...
                          if (node == null)

                               node = root;

                          if(node.children().length() == 0) {
                               node.setChildren(leaf);
                          } else {
                               node.appendChild(leaf);
                          }
                      }

                   

                      private function getNodeByPid(pid:int):XML {

                      /*
                          var x:XMLList = null;

                   

                          if (root.children().length()>0) {
                               x = root.elements("category").(@id==pid);
                          }

                   

                          return x;

                      */

                       // We need to search the whole tree and return any node found (Should be unique!?)

                       // If we had different type nodes we could use root..*.(@id == pid)[0]

                          return root..category.(@id == pid)[0];
                      }