7 Replies Latest reply on Mar 9, 2010 10:15 AM by mkayyali

    parse xml

    decpariem Level 1

      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.

        • 1. Re: parse xml
          Harry Kunz Level 4

          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.

          1 person found this helpful
          • 3. Re: parse xml
            decpariem Level 1

            new.gif

            this is the xml.

            thank you for the reply

            • 4. Re: parse xml
              mkayyali Level 1

              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[i].childNodes;
                             if (menuData[i].nodeName == "vid")
                             {
                                  //Get ID 
                                  idArr.push(menuData[i].attributes.id);
              
                                  //loop for subs
                                  for (j = 0; j <= subNodes.length; j++)
                                  {
                                       if (subNodes[j].nodeName == "submenu")
                                       {
                                            subIDArr.push(subNodes[j].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.

              1 person found this helpful
              • 5. Re: parse xml
                decpariem Level 1

                if you look closely you'll see that i don't have a two level menu.

                if it was i'd know how to handle it.

                this is the actionscript for the first submenu which is not working.new.gif

                • 6. Re: parse xml
                  mkayyali Level 1

                  in your example, we can create new movieclip first then load on it the attached menu, so add this line:

                   

                  this.createEmptyMovieClip("cont",0);
                  

                   

                  then inside your for loop this:

                   

                  cont.attachMovie("subSuperButton","subButton" + j,j,{_x:20, _y:20});
                  cont["subButton" + j].id_txt.text = subNodes[j].attributes.id;
                  cont["subButton" + j].linkPath = subNodes[j].attributes.link;
                  cont["subButton" + j]._y = j * 30;
                  cont["subButton" + j].onRelease = function()
                  {
                       trace("URL Path: " + this.linkPath);
                  };
                  

                  • 7. Re: parse xml
                    mkayyali Level 1

                    all code if you wish is below:

                     

                    //create empty movieclip to load inside it the submenu
                    this.createEmptyMovieClip("cont", 0);
                    
                    //XML
                    var menus:XML = new XML();
                    menus.load("menu.xml");
                    menus.ignoreWhite = true;
                    menus.onLoad = function(success)
                    {
                         if (success)
                         {
                               var menuData = menus.firstChild.childNodes[2].childNodes;
                    
                              for (j = 0; j <= menuData.length; j++)
                              {
                                   if (menuData[j].nodeName == "submenu")
                                   {
                                        cont.attachMovie("subSuperButton","subButton" + j,j,{_x:20, _y:20});
                                        cont["subButton" + j].id_txt.text = menuData[j].attributes.id;
                                        cont["subButton" + j].linkPath = menuData[j].attributes.link;
                                        cont["subButton" + j]._y = j * 30;
                                        cont["subButton" + j].onRelease = function()
                                        {
                                             trace(this.linkPath);
                                        };
                                   }
                              }
                         } else
                         {
                              trace("Error on loading xml");
                         }
                    };
                    

                     

                    i hope this to help you