问题描述
这是我检索使用AS3 E4X一个XML文件的一部分:
this is part of an XML file I retrieve using AS3 E4X:
<links>
<link>
<label>Versions</label>
<href>http://mylink1</href>
</link>
<link>
<label>Configurations</label>
<href>http://myLink2</href>
</link>
</links>
我要检索标签的价值,所以我写的:
I want to retrieve the values of labels, so I write:
document.links.link.label.text();
这将返回VersionsConfigurations。我需要这样的阵列([版本,配置]),但我想不会使用循环。有没有其他办法?
This returns VersionsConfigurations. I need this as Array ([Versions, Configurations]) but I would like not to use a loop. Is there any other way?
推荐答案
那么,这是一个不要在家里尝试这个解决方案,但在这里你。 :)
Well, this is a "don't try this at home" solution, but here you are. :)
您可以使用E4X搜索EX pression做任何你想要的XMLList的节点。
You can use E4X search expression to do whatever you want to nodes of an XMLList.
该工作原理如下: someXMLList(如pression)
,其中EX pression任何AS3 code可以访问每一个节点的属性。并且,无需出线他们的名字的方法。例如,你可以做到以下几点:
This works as follows: someXMLList.(expression)
, where expression is any AS3 code that can access each node's properties and methods with no need of qualifying their names. For instance, you could do the following:
yourXML.descendants("label").(trace("label text: ", text()));
请注意,我使用文本()
这里没有访问。
操作。其实这会返回一个新的XMLList中的所有节点,其中EX pression评估,以真。由于跟踪()
返回void,结果列表将是空的。内部当然还有通过XMLList的是通过调用的后裔()
(或使用 ..
运营商)。
Note that I'm using text()
here with no access .
operations. Actually this will return an new XMLList for all nodes, where expression evaluated to true
. Since trace()
returns void, the resulting list will be empty. Internally there is of course a loop through all nodes of XMLLIst that is created by calling descendants()
(or using ..
operator).
您可以构建您的阵列相同的方式。
You can construct your array the same way.
var doc:XML =
<links>
<link>
<label>Versions</label>
<href>http://mylink1</href>
</link>
<link>
<label>Configurations</label>
<href>http://myLink2</href>
</link>
<link>
<label>A label
with
multiple
line
breaks</label>
<href>http://myLink3</href>
</link>
</links>;
trace(doc.descendants("label").text().toXMLString().split("\n"));
/* Trace output (incorrect):
Versions,Configurations,A label
,with
,multiple
,line
,breaks
*/
var list:Array = [];
doc.descendants("label").(list.push(text().toString()));
trace(list);
/* Trace output (correct):
Versions,Configurations,A label
with
multiple
line
breaks
*/
这是一个XMLList进行一些复杂的搜索时可能有用。然而,在你的情况,我认为你应该使用一个字符串,再presentation或常规EX pression的简单拆分为谢恩建议。
That may be useful when performing some complicated searches on an XMLList. However in your case I think you should instead use simple splitting of a string representation or a regular expression as Shane suggests.
这篇关于E4X / AS3,得到的文本元素的数组不用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!