AS3 XML循环,抓取属性和子项

时间:2012-10-18 15:21:20

标签: xml actionscript-3

这是我的XML文件:

<cm>
    <strategy_description>
        <category name="Account">
            <item name="tree">test1</item>
            <item name="type">test2</item>
        </category>
        <category name="Account2">
            <item name="tree">test1</item>
            <item name="type">test2</item>
        </category>
    </strategy_description>
</cm>

我想输出这样的东西:

Account
tree: test1
type: test2
Account2
tree: test1
type: test2

我做到了这一点,并设法以XML格式跟踪输出所有类别和项目节点:

var categoryList:XMLList = xmlData.strategy_description.category;
for each(var category:XML in categoryList){
    trace(category);
}

1 个答案:

答案 0 :(得分:2)

您需要嵌套循环,例如:

var categoryList:XMLList = xmlData.strategy_description.category;

for each(var category:XML in categoryList)
{
    trace(category.@name);

    for each(var item:XML in category.item)
    {
        trace("  "+item.@name +": "+ item);
    }
}

希望这有帮助!