【发布时间】:2015-02-15 11:48:43
【问题描述】:
如何获取 Haxe Script 中 XML 子节点的数量?
在 AS3 中,我会写 node.children().length()。
假设在 Haxe 中,我有:
var node = FastXML.parse("<node><hello>Hi</hello><world/></node>");
在这种情况下,节点有 2 个孩子。如何在 Haxe 中获得这个?
【问题讨论】:
如何获取 Haxe Script 中 XML 子节点的数量?
在 AS3 中,我会写 node.children().length()。
假设在 Haxe 中,我有:
var node = FastXML.parse("<node><hello>Hi</hello><world/></node>");
在这种情况下,节点有 2 个孩子。如何在 Haxe 中获得这个?
【问题讨论】:
由于Xml 本身是其子节点的可迭代对象,我们可以使用Lambda.count。
using Lambda; //Static Extension http://haxe.org/manual/lf-static-extension.html
class Test {
static function main() {
var xml = Xml.parse("<node><hello>Hi</hello><world/></node>");
var node = xml.firstChild(); //<node>
trace(node.count()); //2
var fast = new haxe.xml.Fast(Xml.parse("<node><hello>Hi</hello><world/></node>"));
var node = fast.node.node; //<node>
trace(node.x.count()); //2
}
}
【讨论】:
我找到了解决办法:
node.descendants().length();
【讨论】: