How To : only attachMovie if having childNodes

I am trying to build xml menu. Some nodes have childNodes some don’t. I have a problem in my array loop : the movieclip that I use to create subMenu attached to the entire array. How can I make that the movieclip that I use for subMenu only attach to the nodes that having childNodes?

Here’s the code :


function buildingMenu(){	
	
	buttonArray = myXml.firstChild.childNodes;
	for (var i=0;i<buttonArray.length;i++)
	{
		var k = this.attachMovie("menuItem", "menuItem"+i, i);		
		mainMenu = buttonArray*;
		k._alpha = 100;
		k._x = Math.round(0 + ((k._width+4)*i));
		k.id = mainMenu.attributes.id;
        k.nameTxt.text = k.id;	
		k._y = 0;
		
		//For sub menu
		subbuttonArray = myXml.firstChild.childNodes*.childNodes;
		//subContain = this.createEmptyMovieClip("subContain"+i, i+1000);
		subContain = this.createEmptyMovieClip("subContain"+i, i+1000);
		
		for(n = 0; n<=subbuttonArray.length-1; n++){
			//trace(subbuttonArray[n])
			subMenu = subbuttonArray[n];
			var m =this.subContain.attachMovie( "sub_menuItem", "sub_menuItem"+n, n+1000);
			m.sub_nameTxt.text = subMenu.attributes.id;
			m._x =  Math.round(0 + ((m._width+4)*i));
			m._y = (k._y - k.box._height)-(m.box._height*n );
			m._visible= true;
			m.onRelease = function(){
            getURL(this.itemUrl, "_blank");
         }
      }//END loop FOR subbuttonArray




Thanks for having a look

can’t you simply check that array length and use a conditional for your loop (trigger it to loop through only if array has a length greater than zero)?

something like this:


var subCount:Number = mySubArray.length;
if(subCount > 0){
//loop through sub items code here
};

does that help?

Yes. It helps. Thanks a lot