xml 如何在没有循环的情况下将简单的 XMLList 转换为字符串数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/583473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:22:55  来源:igfitidea点击:

How can I convert a simple XMLList to an Array of Strings without a loop?

xmlapache-flexarraysactionscriptxmllist

提问by Eric Belair

How can I convert the following XMLList to an Array of Strings without using a loop?

如何在不使用循环的情况下将以下 XMLList 转换为字符串数组?

<labels>
    <label>All</label>
    <label>your</label>
    <label>base</label>
    <label>are</label>
    <label>belong</label>
    <label>to</label>
    <label>us.</label>
</labels>

I want this result:

我想要这个结果:

["All","your","base","are","belong","to","us."]

Right now, I am doing the following:

现在,我正在执行以下操作:

var labelsArray:Array /* of String */ = [];

for each (var labelText:String in labels.label)
{
    labelsArray.push(labelText);
}

I was wondering if there is a simpler way to do this in ActionScript 3.0

我想知道在 ActionScript 3.0 中是否有更简单的方法来做到这一点

回答by Densefog

This works good but uses some odd syntax of the XMLList. The last statement can be placed on one line if desired.

这很好用,但使用了一些奇怪的 XMLList 语法。如果需要,最后一条语句可以放在一行上。

    var labels:XML = <labels>
                    <label>All</label>
                    <label>your</label>
                    <label>base</label>
                    <label>are</label>
                    <label>belong</label>
                    <label>to</label>
                    <label>us.</label>
                </labels>;

var labelsArray:Array /* of String */ = [];

labels.label.
(
              labelsArray.push(toString())
);  

The toString() call can be replaced with an attribute() call to pull out attributes.

toString() 调用可以替换为 attribute() 调用以提取属性。

回答by one giant media

This one works pretty well:

这个效果很好:

public static function xmlListToArray($x:XMLList):Array {           
    var a:Array=[], i:String;
    for (i in $x) a[i] = $x[i];
    return a;
}

回答by dlamblin

Despite the earnest uses of forloops and logically working on the XMLobject as given, this is a job for XMLList.
It would best look something like this:

尽管认真使用了for循环并XML按照给定的逻辑对对象进行了处理,但这对于XMLList.
最好看起来像这样:

var xml:XML = 
<labels>
    <label>All</label>
    <label>your</label>
    <label>base</label>
    <label>are</label>
    <label>belong</label>
    <label>to</label>
    <label>us.</label>
</labels>
;
var list:XMLList = xml.label;
var labels:XMLList = list.text(); //Optional
trace(list[0]);
trace(list[3]);
trace(list[6]);

This would output:

这将输出:

All
are
us.

I've confirmed this in flash myself. Personally it makes sense to me to use the optional line, and reference labels[0]etc. but that's not needed here.

我自己已经在 flash 中确认了这一点。就我个人而言,使用可选行和引用labels[0]等是有意义的,但这里不需要。

I know you're asking for an array of strings as the output, but basically I'm asking you why you can't just use the array accessors of an XMLList object.

我知道您需要一个字符串数组作为输出,但基本上我是在问您为什么不能只使用 XMLList 对象的数组访问器。

Here's a fine walk-through on that: Senocular on E4X.

这是一个很好的演练E4X 上的 Sencular

回答by Richard Szalay

Your current implementation is more than sufficient. The only optimisation you could make (though I wouldn't bother unless you are using Vector.<>) is to pass in the initial capacity into the Array constructor:

您当前的实施已绰绰有余。您可以进行的唯一优化(尽管除非您使用 Vector.<> 否则我不会打扰)是将初始容量传递给 Array 构造函数:

var xmlLabels : XMLList = labels.label;
var labelsArray:Array /* of String */ = new Array(xmlLabels.length);

var index : int = 0;

for each (var labelText:String in xmlLabels)
{
    labelsArray[index++] = labelText;
}

回答by dirkgently

forloops are extremely fast in AS. Why'd you need that? But you could give this a try:

for循环在 AS 中非常快。你为什么需要那个?但是你可以试试这个:

private function toArray():void {
    var xml:XML = <labels>
               <label>all</label>
               <label>your</label>
              </labels>;

    var array:* = xml.label.text().toXMLString().split("\n") ;
    trace(array as Array);
}

回答by one giant media

i feel like there's a one-liner for this out there somewhere... oh well.

我觉得在某个地方有一个单衬……哦,好吧。

question: why does .length fail here? (always 0)

问题:为什么 .length 在这里失败?(始终为 0)

 public static function xmlListToArray($x:XMLList):Array {
        var t:int = $x.length;
        var a:Array=new Array(t), i:int;
        for (i = 0; i < t; ++i) a[i] = $x[i];
        return a;
    }