Skip to main content
Inspiring
June 7, 2007
Question

Going through entire XML structure

  • June 7, 2007
  • 4 replies
  • 551 views
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!
This topic has been closed for replies.

4 replies

Inspiring
June 8, 2007
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
hciguyAuthor
Inspiring
June 8, 2007
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!!!
Participant
June 7, 2007
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!
Participant
June 7, 2007
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!
hciguyAuthor
Inspiring
June 7, 2007
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!
hciguyAuthor
Inspiring
June 7, 2007
Anyone? Every time I ask XML questions no one responds lol