> From: Taka Kojima <[email protected]>

> To: Flash Coders List <[email protected]>
> Sent: Sunday, August 14, 2011 4:24 AM
> Subject: Re: [Flashcoders] Simplify XML Call

> function getNumItems(level:int, xml:XML):int{
> var levelXML:XML = xml.menu;
> for(var i:int = 0; i < level; i ++){
> levelXML = levelXML.item[whichItems[level]];
> }
> return levelXML.length();
> }

I modified your code to correct an oversight or two like this:

        private function getNumItems(level, xml):int
        {
            var levelXML:XMLList = xml.menu;
            for(var i:int = 0; i < level - 1; i ++)
            {
           
     levelXML = levelXML.item[whichItems[i]] as XMLList;
                trace(String(levelXML));
            }
            return levelXML.length();
        }

The problem here is that it traces null because you can't convert a string into 
an XMLList and, of course, you can't assign a string to an XMLList either, 
which is why I tried the conversion. (I also tried casting levelXML as a 
wildcard with the same result: null.)

Regarding Jason's code, 

        private function countItems(xmlNode:XML):void
        {
            var total:int = 0;
            var xmlChildren:XMLList = xmlNode.children();
            if(xmlChildren.length() > 0)
            {
                for each (var xmlNode:XML in xmlChildren)
                {
                    total++;
                    countItems(xmlNode); //The recursive call
                }
            }
            trace("totalItems2: ", total);
        }

it doesn't do what I need. What I need it to target a specific sequence of 
nodes. Let's suppose I have the following xml:

<data>
    <item>
        <label><![CDATA[Bracelets]]></label>
        <item>
            <label><![CDATA[Hook Bracelets]]></label>
            <item>
                <label><![CDATA[one]]></label>
            </item>
            <item>
                <label><![CDATA[two]]></label>
                <item>
                    <label><![CDATA[three]]></label>
                </item>
            </item>
        </item>
</data>

Perhaps it would clarify things to state I'm building a drop-down menu. So the 
client mouses over "Bracelets". Out pops "Hook Bracelets". She mouses over that 
and out pops "one" and "two". In order to accomplish this, I need to discover 
which node is being moused over, in this case:

item[0].item[0]

but clearly it could just as easily be:

item[46].item[27]

and I could care less about the rest of the tree. Having said as much, my 
revised code:

            var x:*;
            x = xml.menu.item[whichItems[0]];
            var levelsLeft:int = level - 2;
            var q:int;
            while(levelsLeft--)
            {
                q++;
                x = x.item[whichItems[q]];
            }
            trace('xxx', x.item.length());

does, in fact, appear to work :))
Thanks again,
John
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to