Another XML nest, HELP!

  • telly699
  • Born
  • Born
  • telly699
  • Posts: 2
  • Loc: Chicago

Post 3+ Months Ago

Ok, so I am using a great tutorial for a dynamic XML menu. It works except when I try to pull data from a nested xml document which is being created for me.

Anyone out there understand the nested loop, because I am having troubles! This is the current actionscrip:

Code: [ Select ]
var menu_xml:XML = new XML();
menu_xml.ignoreWhite = true;
menu_xml.onLoad = function(success:Boolean){
  if(success){
    var curNode:XMLNode = new XMLNode();//hold the current item node its working with
var item:MovieClip = new MovieClip();//will be mHeading once it is attached, saves alot of typing
var curSubNode:XMLNode = new XMLNode();//will become the current subitem node its working with
var subItem:MovieClip = new MovieClip();//will be mSub once it is attached
var subLen:Number = new Number();//will be the number of mSubs attached into each mHeading
  
var menu:XMLNode = menu_xml.firstChild;//the first child of the xml document <menu>

var itemLen = menu.childNodes.length;//the number of <item>'s in the document
numMenuParts = itemLen;//used for opening and closing the submenus
   
//loop through all of the <item>'s
for(i:Number=0;i<itemLen;i++){
  curNode = menu.childNodes[i];//shortcut to save on typing
item = holder_mc.attachMovie("mHeading_mc", "mHeading"+i+"_mc", holder_mc.getNextHighestDepth(), {_y:headingHeight*i});
item.order = i;//the order it appears in the menu from top to bottom, zero based
item.orgY = headingHeight*i;//the original placement, where it moves back to when no submenus are open
//gets the parameters from the xml, if there isn't that parameter in the xml (href and win for a AS call item) it will return undefined
item.href = curNode.attributes.href;
item.win = curNode.attributes.win;
item.param = curNode.attributes.param;  
item.heading_txt.text = curNode.attributes.txt;

//if the curent node has children (the item has subitems)
if(curNode.hasChildNodes()){
  subLen = curNode.childNodes.length;//how many subitems the item has
  item.numOfKids = subLen;//used to know how far down the mask should move
  item.subMask_mc._height = subHeight*subLen;//make the subMask big enough to cover all the subs
  item.subMask_mc._visible = false;//hide the original mask because it has to be duplicated later on
  //loop through subs
  for(j:Number=0;j<subLen;j++){
    curSubNode = curNode.childNodes[j];//shortcut to the current subitem node
    subItem = item.attachMovie("mSub_mc", "mSub"+j+"_mc", item.getNextHighestDepth(), {_y:(subHeight*j)+headingHeight});//attaches mSub
    subItem.href = curSubNode.attributes.href;//same as item.href
    subItem.win = curSubNode.attributes.win;//same as item.win
    subItem.param = curSubNode.attributes.param;//same as item.param
    subItem.name_txt.text = curSubNode.attributes.txt;//same as item.heading_txt
    dupedMask = item.subMask_mc.duplicateMovieClip("subMask"+j+"_mc", item.getNextHighestDepth());//duplicate the original mask because ever sub needs its own mask
    subItem.setMask(dupedMask);//set the duplicated mask to mask the attached mSub_mc
   
    //subitem onPress
    subItem.bg_mc.onPress = function(){
      //have to use relative syntax to get to the movieclips variables
      if(this._parent.href == undefined){//if href is undefined means its an AS call
        menuActions(this._parent.param);//send our function the parameters from the xml node
      }
      else{//if it is a link
        getURL(this._parent.href,this._parent.win);//getURL(link location,window,method); we don't need the method so we supply the href and window
      };
    };//end sub onPress
  };
  //Item onPress
  item.bg_mc.onPress = function(){
    openSubs(this._parent);//since there are subs we call the openSubs function passing it the item movieclip to be opened
  };
}

else{//the current item node has no children (heading has no sub items)
  item.subMask_mc._visible = false;//hide the mask
  item.bg_mc.onPress = function(){
    if(subOpen){ closeSubs(curOpen, easeType, time, useSeconds, restoring, this._parent) };//if there's a submenu open, close it
    //same as the subitem on press function
    if(this._parent.href == undefined){
      menuActions(this._parent.param);
    }
    else{
      getURL(this._parent.href,this._parent.win);
    };
  };
};//end childNode if/then


};//end item looper
  }//end successful load
  else{
    trace("oops");//if the xml didn't load right, say so. Should be something other than trace once used live
  };
};//end xml load
menu_xml.load("http://69.20.127.43/iccorp/busesXMLProvider?type=school");//load our menu's xml file
  1. var menu_xml:XML = new XML();
  2. menu_xml.ignoreWhite = true;
  3. menu_xml.onLoad = function(success:Boolean){
  4.   if(success){
  5.     var curNode:XMLNode = new XMLNode();//hold the current item node its working with
  6. var item:MovieClip = new MovieClip();//will be mHeading once it is attached, saves alot of typing
  7. var curSubNode:XMLNode = new XMLNode();//will become the current subitem node its working with
  8. var subItem:MovieClip = new MovieClip();//will be mSub once it is attached
  9. var subLen:Number = new Number();//will be the number of mSubs attached into each mHeading
  10.   
  11. var menu:XMLNode = menu_xml.firstChild;//the first child of the xml document <menu>
  12. var itemLen = menu.childNodes.length;//the number of <item>'s in the document
  13. numMenuParts = itemLen;//used for opening and closing the submenus
  14.    
  15. //loop through all of the <item>'s
  16. for(i:Number=0;i<itemLen;i++){
  17.   curNode = menu.childNodes[i];//shortcut to save on typing
  18. item = holder_mc.attachMovie("mHeading_mc", "mHeading"+i+"_mc", holder_mc.getNextHighestDepth(), {_y:headingHeight*i});
  19. item.order = i;//the order it appears in the menu from top to bottom, zero based
  20. item.orgY = headingHeight*i;//the original placement, where it moves back to when no submenus are open
  21. //gets the parameters from the xml, if there isn't that parameter in the xml (href and win for a AS call item) it will return undefined
  22. item.href = curNode.attributes.href;
  23. item.win = curNode.attributes.win;
  24. item.param = curNode.attributes.param;  
  25. item.heading_txt.text = curNode.attributes.txt;
  26. //if the curent node has children (the item has subitems)
  27. if(curNode.hasChildNodes()){
  28.   subLen = curNode.childNodes.length;//how many subitems the item has
  29.   item.numOfKids = subLen;//used to know how far down the mask should move
  30.   item.subMask_mc._height = subHeight*subLen;//make the subMask big enough to cover all the subs
  31.   item.subMask_mc._visible = false;//hide the original mask because it has to be duplicated later on
  32.   //loop through subs
  33.   for(j:Number=0;j<subLen;j++){
  34.     curSubNode = curNode.childNodes[j];//shortcut to the current subitem node
  35.     subItem = item.attachMovie("mSub_mc", "mSub"+j+"_mc", item.getNextHighestDepth(), {_y:(subHeight*j)+headingHeight});//attaches mSub
  36.     subItem.href = curSubNode.attributes.href;//same as item.href
  37.     subItem.win = curSubNode.attributes.win;//same as item.win
  38.     subItem.param = curSubNode.attributes.param;//same as item.param
  39.     subItem.name_txt.text = curSubNode.attributes.txt;//same as item.heading_txt
  40.     dupedMask = item.subMask_mc.duplicateMovieClip("subMask"+j+"_mc", item.getNextHighestDepth());//duplicate the original mask because ever sub needs its own mask
  41.     subItem.setMask(dupedMask);//set the duplicated mask to mask the attached mSub_mc
  42.    
  43.     //subitem onPress
  44.     subItem.bg_mc.onPress = function(){
  45.       //have to use relative syntax to get to the movieclips variables
  46.       if(this._parent.href == undefined){//if href is undefined means its an AS call
  47.         menuActions(this._parent.param);//send our function the parameters from the xml node
  48.       }
  49.       else{//if it is a link
  50.         getURL(this._parent.href,this._parent.win);//getURL(link location,window,method); we don't need the method so we supply the href and window
  51.       };
  52.     };//end sub onPress
  53.   };
  54.   //Item onPress
  55.   item.bg_mc.onPress = function(){
  56.     openSubs(this._parent);//since there are subs we call the openSubs function passing it the item movieclip to be opened
  57.   };
  58. }
  59. else{//the current item node has no children (heading has no sub items)
  60.   item.subMask_mc._visible = false;//hide the mask
  61.   item.bg_mc.onPress = function(){
  62.     if(subOpen){ closeSubs(curOpen, easeType, time, useSeconds, restoring, this._parent) };//if there's a submenu open, close it
  63.     //same as the subitem on press function
  64.     if(this._parent.href == undefined){
  65.       menuActions(this._parent.param);
  66.     }
  67.     else{
  68.       getURL(this._parent.href,this._parent.win);
  69.     };
  70.   };
  71. };//end childNode if/then
  72. };//end item looper
  73.   }//end successful load
  74.   else{
  75.     trace("oops");//if the xml didn't load right, say so. Should be something other than trace once used live
  76.   };
  77. };//end xml load
  78. menu_xml.load("http://69.20.127.43/iccorp/busesXMLProvider?type=school");//load our menu's xml file


That code works great with the following simple XML structure:

Code: [ Select ]
<menu>
<item txt="Trucks">
    <subitem txt="model1" href="http://www.model1.com" win="_BLANK" />
    <subitem txt="model2" href="http://www.model2.com" win="_BLANK" />
    <subitem txt="model3" href="http://www.model3.com" win="_BLANK" />
</item>
</menu>
  1. <menu>
  2. <item txt="Trucks">
  3.     <subitem txt="model1" href="http://www.model1.com" win="_BLANK" />
  4.     <subitem txt="model2" href="http://www.model2.com" win="_BLANK" />
  5.     <subitem txt="model3" href="http://www.model3.com" win="_BLANK" />
  6. </item>
  7. </menu>


So I am trying to get that Flash script to work with this much more complex XML structure... I am trying to populate the menu with data from the <classes> node and not the other two.

Code: [ Select ]
<data>
+ <applications></applications>
- <classes>
   + <class id="S_01">  
   + <class id="S_02">
   - <class id="S_03">
     <name>home</name>
     - <class_series>
        + <series id="S_03_Green"></series>
        + <series id="S_03_Red"></series>
        - <series id="S_03_Blue">
           <name> blue </name>
          </series>
       </class_series>
    </class>
  </classes>
+<fly></fly>
</data>
  1. <data>
  2. + <applications></applications>
  3. - <classes>
  4.    + <class id="S_01">  
  5.    + <class id="S_02">
  6.    - <class id="S_03">
  7.      <name>home</name>
  8.      - <class_series>
  9.         + <series id="S_03_Green"></series>
  10.         + <series id="S_03_Red"></series>
  11.         - <series id="S_03_Blue">
  12.            <name> blue </name>
  13.           </series>
  14.        </class_series>
  15.     </class>
  16.   </classes>
  17. +<fly></fly>
  18. </data>


"home" should be the parent while the colors "blue" and the other colors should be the children... i am a bit lost on pulling in nested XML data.

Any help would be really apreciated,
-jerad
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Would it help to see the document mapped in terms of the Flash API ?

This is how each node would be accessed from the XML object.

Code: [ Select ]
<firstChild>
    <firstChild.childNodes[0]/>
    <firstChild.childNodes[1]>
        <firstChild.childNodes[1].childNodes[0] attributes.id="S_01">  
        <firstChild.childNodes[1].childNodes[1] attributes.id="S_02">
        <firstChild.childNodes[1].childNodes[2] attributes.id="S_03">
            <firstChild.childNodes[1].childNodes[2].childNodes[0]>home</childNodes[0]>
            <firstChild.childNodes[1].childNodes[2].childNodes[0].childNodes[0]>
                <firstChild.childNodes[1].childNodes[2].childNodes[0].childNodes[0].childNodes[0] attributes.id="S_03_Green" />
                <firstChild.childNodes[1].childNodes[2].childNodes[0].childNodes[0].childNodes[1] attributes.id="S_03_Red" />
                <firstChild.childNodes[1].childNodes[2].childNodes[0].childNodes[0].childNodes[2] attributes.id="S_03_Blue">
                    <firstChild.childNodes[1].childNodes[2].childNodes[0].childNodes[0].childNodes[2].childNodes[0]> blue </childNodes[0]>
                </childNodes[2]>
            </childNodes[0]>
        </childNodes[0]>
    </childNodes[1]>
    <childNodes[2]></childNodes[2]>
</firstChild>
  1. <firstChild>
  2.     <firstChild.childNodes[0]/>
  3.     <firstChild.childNodes[1]>
  4.         <firstChild.childNodes[1].childNodes[0] attributes.id="S_01">  
  5.         <firstChild.childNodes[1].childNodes[1] attributes.id="S_02">
  6.         <firstChild.childNodes[1].childNodes[2] attributes.id="S_03">
  7.             <firstChild.childNodes[1].childNodes[2].childNodes[0]>home</childNodes[0]>
  8.             <firstChild.childNodes[1].childNodes[2].childNodes[0].childNodes[0]>
  9.                 <firstChild.childNodes[1].childNodes[2].childNodes[0].childNodes[0].childNodes[0] attributes.id="S_03_Green" />
  10.                 <firstChild.childNodes[1].childNodes[2].childNodes[0].childNodes[0].childNodes[1] attributes.id="S_03_Red" />
  11.                 <firstChild.childNodes[1].childNodes[2].childNodes[0].childNodes[0].childNodes[2] attributes.id="S_03_Blue">
  12.                     <firstChild.childNodes[1].childNodes[2].childNodes[0].childNodes[0].childNodes[2].childNodes[0]> blue </childNodes[0]>
  13.                 </childNodes[2]>
  14.             </childNodes[0]>
  15.         </childNodes[0]>
  16.     </childNodes[1]>
  17.     <childNodes[2]></childNodes[2]>
  18. </firstChild>
  • telly699
  • Born
  • Born
  • telly699
  • Posts: 2
  • Loc: Chicago

Post 3+ Months Ago

Anything helps, thanks!

So I tried something like:

var menu:XMLNode = menu_xml.firstChild.childNodes;

to try and pull in just that specific node but it returned an error:

67: Type mismatch in assignment statement: found Array where XMLNode is required.
var menu:XMLNode = menu_xml.firstChild.childNodes;

Is that not the correct place to specify the node? What defines an array from an XMLNode?

Thnaks so much! I am racking my brain trying to understand all this!
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

An XMLNode must be a single node, it can not be multiple nodes like childNodes returns.

Somthing I missed in the mapping is accessing the value of text nodes.
The text inside a node is considered the firstChild of the node that contains it.

For instance in,
Code: [ Select ]
<firstChild.childNodes[1].childNodes[2].childNodes[0]>
  home
</childNode s[0]>
  1. <firstChild.childNodes[1].childNodes[2].childNodes[0]>
  2.   home
  3. </childNode s[0]>


You would access "home" with somthing like this
Code: [ Select ]
var node:XMLNode = firstChild.childNodes[1].childNodes[2].childNodes[0];

var nodeText:String = node.firstChild.nodeValue;
  1. var node:XMLNode = firstChild.childNodes[1].childNodes[2].childNodes[0];
  2. var nodeText:String = node.firstChild.nodeValue;

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 14 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.