0

如何填充这样格式的数组?:

var names:Array = [{label:"JAMES"}, {label:"JANE"}, {label:"JAMEL"}...];

来自像这样的 XML?:

<a>
  <ap>
     <name>JAMES</name>
     <age>36</age>
  </ap>
</a>

这适用于自动完成组件。

更新,从更适合我技能的东西开始。这个:

<list>
  <label>ALPHA</label>
  <label>ALLAN</label>
  <label>ANTARES</label>
  <label>...</label>
</list>

对此:

var list:Array = [{label:"ALPHA"}, {label:"ALLAN"}, {label:"ANTARES"}...];
4

1 回答 1

2
const xml:XML =
    <list>
        <label>ALPHA</label>
        <label>ALLAN</label>
        <label>ANTARES</label>
        <label>...</label>
    </list>;

const list:Array = new Array();

//xml..label.(trace(text()));
xml..label.(list.push({label:text()}));

// now we have an array:
// [{label:"ALPHA"}, {label:"ALLAN"}, {label:"ANTARES"}, {label:"..."}]

我建议阅读 AVM2 规范并特别注意命名空间。严重地。这很有趣,很有趣!


嗯...另一种无聊的方式:

const list:Array = new Array();
const xml:XML =
    <list>
        <label>ALPHA</label>
        <label>ALLAN</label>
        <label>ANTARES</label>
        <label>...</label>
    </list>;

const labels:XMLList = xml..label;
for each(var node:XML in labels)
{
    trace(node);
    var arrayItem:Object = new Object();
    arrayItem.label = node.text(); // or node.toString() or .toJSON() or .to...
    arrayItem.name = node.name();

    // added only for debug-trace:
    arrayItem.toString = function():String
    {
        var result:String = '{', delimiter:String = '';
        for(var key:String in this)
            if(key !== 'toString')
                result += delimiter + key + ':"' + this[key] + '"',
                    delimiter ||= ', ';
        return result + '}';
    }

    // add item to list:
    list.push(arrayItem);
}

trace(list);
于 2013-08-28T08:38:06.427 回答