how do I find the number of xml children in AS3

只谈情不闲聊 提交于 2020-01-04 09:18:21

问题


so live docs says this for calling .lenght() on an XML object

For XML objects, this method always returns the integer 1. The length() method of the XMLList class returns a value of 1 for an XMLList object that contains only one value.

i called it on an xml that looked like this:

<xml>
<picture>1</picture>
<picture>2</picture>
</xml>

i tried myXML.lenght() and it reallt returned 1. how do i get the number of children in my xml?


回答1:


Try

var length:int = myXML.children().length();

Also, this is the method I use to make sure the children are really Elements, and not just Text Nodes.

    public static function getNumberChildElements(node:XML):int{
        var count:int = 0;
        for (var i:int=0; i<node.children().length(); i++){
            if (node.children()[i].nodeKind() == "element")
                count++;
        }
        return count;
    }



回答2:


Try:

var totPictures=myXML.picture.length();
trace(totPictures); //2



回答3:


Also myXML.*.length() will work




回答4:


I think this is the right way.

var totItems:int = myXML.elements().children().length();
trace(totItems);



回答5:


Try the following:

var num:int = myXml.length();


来源:https://stackoverflow.com/questions/2655514/how-do-i-find-the-number-of-xml-children-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!