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

发布于 2024-07-14 14:57:14 字数 665 浏览 14 评论 0原文

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

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

我想要这个结果:

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

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

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

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

我想知道在 ActionScript 3.0 中是否有更简单的方法来执行此操作

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

<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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

仅一夜美梦 2024-07-21 14:57:14

这很有效,但使用了一些奇怪的 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())
);  

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

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

    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.

岁月染过的梦 2024-07-21 14:57:14

这个效果很好:

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

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;
}
梨涡少年 2024-07-21 14:57:14

您当前的实现已经足够了。 您可以进行的唯一优化(尽管除非您使用 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;
}

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:

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;
}
听闻余生 2024-07-21 14:57:14

尽管认真使用了 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]);

这将输出:

All
are
us.

我自己在闪存中确认了这一点。 就我个人而言,使用可选行和引用标签[0]等对我来说是有意义的,但这里不需要这样做。

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

这里有一个很好的演练:E4X 上的 Senulous

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

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.

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.

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

蓝眼睛不忧郁 2024-07-21 14:57:14

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);
}

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

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);
}
帅的被狗咬 2024-07-21 14:57:14

我觉得在某个地方有一个关于这个的俏皮话……哦,好吧。

问题:为什么 .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;
    }

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

question: why does .length fail here? (always 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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文