Skip to main content
Known Participant
March 9, 2010
Question

parse xml

  • March 9, 2010
  • 2 replies
  • 851 views

hi. i am trying to make a drop down menu with submenus with data loaded from xml.

i managed to parse only the first child an then i get undefined.

the xml format is something like this:

<menu>

    <menu>

         <menu>

               <item></item>

          </menu>

      </menu>

</menu>

i use a for loop for the first child and on release event i call a function submenu,

and a for(i=0;i<myXML.childNodes.length;i++) but it's not working.

Any help will be appreciated.

This topic has been closed for replies.

2 replies

mkayyali
Inspiring
March 9, 2010
Inspiring
March 9, 2010

You have to first get the root node (top level "menu" node) by getting the firstChild of your XML object like this:

var cNode:XMLNode = oXml.firstChild;

var nChildren:Number = cNode.childNodes.length;

You were looping at the root that's why you only had one node returned. Use nChildren as delimiter to loop through the sibling nodes under first menu node. Can you give the exact XML content? Because if it only looks like that, you could simply access the leaf node by using firstChild.firstChild.firstChild.fiirstChild.firstChild.nodeValue. By the way, it's a bad idea to have children nodes named the same as their parent nodes.

decpariemAuthor
Known Participant
March 9, 2010

this is the xml.

thank you for the reply

mkayyali
Inspiring
March 9, 2010

here we are:

//Vars var idArr:Array = new Array(); var subIDArr:Array = new Array(); //XML var menus:XML = new XML(); menus.load("menu.xml"); menus.ignoreWhite = true; menus.onLoad = function(success) {      if (success)      {           var menuData = menus.firstChild.childNodes;           for (i = 0; i < menuData.length; i++)           {                subNodes = menuData.childNodes;                if (menuData.nodeName == "vid")                {                     //Get ID                     idArr.push(menuData.attributes.id);                     //loop for subs                     for (j = 0; j <= subNodes.length; j++)                     {                          if (subNodes.nodeName == "submenu")                          {                               subIDArr.push(subNodes.attributes.id);                          }                     }                }           }      } else      {           trace("Error on loading xml");      }      trace("Main ID: " + idArr);      trace("Main ID: " + subIDArr); };

but my opinion for you to change the attribute name (id) to anything else like ( Id, ID ) as id is a reserved for use by the ActionScript language.