2 Replies Latest reply on Oct 9, 2009 8:55 AM by RossRitchey

    XML Iteration

    RossRitchey Level 4

      Alright, I don't know why, but I am having a hard time coming up with a simple solution to iterating through my XML and pulling data out.

       

      A portion of the XML:

       

       

      <Style_3>
           <Use>
                <Skin>TRUE</Skin>
                <Gloss>TRUE</Gloss>
           </Use>
           <Tab>
                <Up>
                     <Background_Color>0xFFFFFF</Background_Color>
                     <Background_Alpha>1</Background_Alpha>
                </Up>
           </Tab>
      </Style_3>
      

       

       

      Now.  I am looking to iterate through this (and I crushed it down to just the background color/alpha information for the sake of this) and put the information into an Object that will look like:

       

      myObject["Use_Skin"] = "TRUE"

      myObject["Use_Glossary"] = "TRUE"

      myObject["Tab_Up_Background_Color"] = "0xFFFFFF";

      etc..

       

      The XML structure is able to go another level deeper as well (so "Tab_Up_Bar_Background_Color"), but I don't want to hard code multiple for() loops to iterate through it.

       

      The previous version of the style sheet used the following function for iteration, but there must be a more efficient way of doing things, as well as a way to be more flexible to the depth of the child nodes.

       

      Function:

       

      private function parseToASM():void
      {
           var styleName:String;
           
           for each(var i:XML in _xmlLoaded.children())
           {
                for each(var p:XML in i.children())
                {
                     styleName = i.name()+"_"+p.name();
                                              
                     for each(var c:XML in p.children())
                     {
                          
                          if(c.name() == "" || c.name() == null)
                          {
                               _asm.setStyle(styleName,p.toString());
                          }
                          else
                          {
                               for each(var d:XML in c.children())
                               {
                                    styleName = i.name()+"_"+p.name()+"_"+c.name();
                                    
                                    if(d.name() == "" || d.name() == null)
                                    {
                                         _asm.setStyle(styleName,c.toString());
                                    }
                                    else
                                    {
                                         styleName = i.name()+"_"+p.name()+"_"+c.name()+"_"+d.name();
                                         _asm.setStyle(styleName,d.toString());
                                    }
                               }
                          }
                     }
                }
           }
      }
      

       

      Any thoughts/ideas are very welcome.

        • 1. Re: XML Iteration
          RossRitchey Level 4

          ||BUMP||

          • 2. Re: XML Iteration
            RossRitchey Level 4

            Alright, I broke the loop into a function which calls itself for iteration.

             

            private function parseToASM():void
            {
                 var styleName:String;
                 
                 for each(var i:XML in _xmlLoaded.children())
                 {
                      styleName = i.name();
                      parseChild(i,styleName);
                 }
            }
                      
            private function parseChild(child:XML,name:String):void
            {
                 if(child.hasComplexContent())
                 {
                      for each(var p:XML in child.children())
                      {
                           var newName:String = name+"_"+p.name();
                           parseChild(p,newName);
                      }
                 }
                 else
                 {
                      _asm.setStyle(name,child.toString());
                 }
            }