8 Replies Latest reply on Jun 9, 2007 12:57 PM by Newsgroup_User

    Going through entire XML structure

    hciguy
      Hi guys,

      I made the following function to recurse through an XML file:

      private function parseMenuData(menuData:XML):void{

      for each(var menuItem:XML in menuData.elements()){

      trace("-----------------------------------------------------------");
      trace("Menuitem " + menuItem.@name + "has " + menuItem.children().length() + " children.");

      if(menuItem.children().length() > 0){

      parseMenuData(menuItem);

      }else{
      trace("No children here");
      }

      }
      }

      No where I am getting stuck is how to store the structure. So if menuItem.children().length() > 0 then I know that node has children. If my structure was only one level, it'd be easy to go through the whole list and store it into an array. I am sure I can use a multi-dimensional array but not sure how to load it all in there since I don't know how many levels deep the XML file is I am loading....

      Does that make sense? Any pointers? I'm not asking someone to write code for me but just make help point me in the right direction that I need to go for my own code...

      Thanks!
        • 1. Re: Going through entire XML structure
          hciguy Level 1
          Anyone help guide me in the right direction of how to read an entire XML structure into a multi-dimensional array?

          I know I am missing some logic somewhere.....

          Thanks so much!
          • 2. Re: Going through entire XML structure
            hciguy Level 1
            Anyone? Every time I ask XML questions no one responds lol
            • 3. Going through entire XML structure
              _R_
              You probably don't get many responses because it's not a quick & easy answer.

              There are several examples of XML parsers on FlashKit.

              The biggest issue with parsing the XML is the that a node could have a child node could have children nodes, could have child nodes...and on and on...

              Whenever I'm looking for something specific (in Flash 8), I use XFactorStudio's XPath. That lets me look for what I want without having to know exactly where it is.

              Otherwise, every project I do is Flash with an XML backend and it's not something that one can just quickly type up in a forum. Check your help files and look for examples.

              Sorry I couldn't help more, but good luck!
              • 4. Re: Going through entire XML structure
                _R_ Level 1
                You probably don't get many responses because it's not a quick & easy answer.

                There are several examples of XML parsers on FlashKit.

                The biggest issue with parsing the XML is the that a node could have a child node could have children nodes, could have child nodes...and on and on...

                Whenever I'm looking for something specific (in Flash 8), I use XFactorStudio's XPath. That lets me look for what I want without having to know exactly where it is.

                Otherwise, every project I do is Flash with an XML backend and it's not something that one can just quickly type up in a forum. Check your help files and look for examples.

                Sorry I couldn't help more, but good luck!
                • 5. Re: Going through entire XML structure
                  Level 7
                  hciguy wrote:
                  > Hi guys,
                  >
                  > I made the following function to recurse through an XML file:
                  >
                  > private function parseMenuData(menuData:XML):void{
                  >
                  > for each(var menuItem:XML in menuData.elements()){
                  >
                  > trace("-----------------------------------------------------------");
                  > trace("Menuitem " + menuItem.@name + "has " + menuItem.children().length()
                  > + " children.");
                  >
                  > if(menuItem.children().length() > 0){
                  >
                  > parseMenuData(menuItem);
                  >
                  > }else{
                  > trace("No children here");
                  > }
                  >
                  > }
                  > }
                  >
                  > No where I am getting stuck is how to store the structure. So if
                  > menuItem.children().length() > 0 then I know that node has children. If my
                  > structure was only one level, it'd be easy to go through the whole list and
                  > store it into an array. I am sure I can use a multi-dimensional array but not
                  > sure how to load it all in there since I don't know how many levels deep the
                  > XML file is I am loading....
                  >
                  > Does that make sense? Any pointers? I'm not asking someone to write code for
                  > me but just make help point me in the right direction that I need to go for my
                  > own code...
                  >
                  > Thanks!
                  >
                  From a programmer's perspective, you need a tree!
                  that's a lot of writing of 1 class, to implement storage needs, but
                  after that you could reuse it.btw its not a binary tree, but an N level
                  tree you would use.
                  //doesn't show how to create one, just how to move around it
                  http://en.wikipedia.org/wiki/Tree_traversal.
                  any beginner's programming book for most languages should show
                  an example of a binary or n-tree algorithm.

                  create a class called node
                  inside it create a member/property/variable to hold the data you want
                  for that object, ie if it has a text/string label, picture, etc..

                  then create another member/property/variable to hold array of objects
                  that are also nodes , this should ideally be pointers, obtained
                  whenever you call

                  object = new node(); // this should go in a function called addchild

                  when you create the 1st node, don't forgot to keep pointer to it.
                  ie in whatever other class you call to create the 1st node, save the
                  pointer to it, and call it something like root.
                  then whenver you go back and add child use root pointer and call its
                  addchild function

                  create another member/variable/property to hold the # of children in it,
                  just the direct children, doesn't have to track the children's children.
                  as you add to your array of children increment the count.

                  to traverse the tree decide if you want to go down or across 1st.
                  ie
                  ROOT
                  / / \
                  child1 child2 child3
                  / \ \
                  child1a child1b child2a


                  then write you code to do so
                  • 6. Re: Going through entire XML structure
                    hciguy Level 1
                    So basically I would be creating a bunch of properties/objects to hold the various information that makes up the tree: each node, the attributes of each node, the place of that node in the tree. Is that correct?

                    Thanks so much for your help!!!
                    • 7. Re: Going through entire XML structure
                      hciguy Level 1
                      Or wait...so you're saying two classes:

                      1. Class which is "node," stores the tree object, and this is an object of a node which is created for each node of the XML structure?

                      2. Class which is the one which actually creates each node and names them in a tree-like manner so that it can be traversed?

                      Sorry if I am way off but just trying to write my mind around the model.

                      Thanks again!
                      • 8. Re: Going through entire XML structure
                        Level 7
                        hciguy wrote:
                        > Or wait...so you're saying two classes:
                        >
                        > 1. Class which is "node," stores the tree object, and this is an object of a
                        > node which is created for each node of the XML structure?
                        >
                        > 2. Class which is the one which actually creates each node and names them in a
                        > tree-like manner so that it can be traversed?
                        >
                        > Sorry if I am way off but just trying to write my mind around the model.
                        >
                        > Thanks again!
                        >

                        //the nodes link together to form a tree, just like a real tree has
                        branches and leaves//

                        1 class called node, inside the class node are properties for each
                        node.

                        inside the program you need to create at least 1 instance to start off
                        and call it root. save the pointer you get to it into a variable
                        ie
                        class node
                        ....
                        public var root : node // humm i assume you can do this
                        //actionscript but not sure haven't tried it.

                        then create some methods like addNode or AddChildNode

                        as long as you have the original root pointer/object you can get to the
                        other objects, without it you cannot, since each object is created and
                        referenced from its parent object, which if followed back logically
                        enough would be the root.

                        Don't overthink the problem just take it 1 step at a time!
                        start with 1 piece and build...
                        ie, figure out how to create the node , and then
                        addchildnodes to it.

                        as you add child you can set properties for that child

                        then after that, figure out how to move/transverse the tree
                        either going all the way across at 1 level or probably going all the way
                        down and then accross eg.. find all the children of the 1st child, and
                        then all the children of the 2nd child.

                        Hope this helps.