从一些xml我想要找到具有特定属性和值的项目.
这里是xml的例子:@H_502_3@
<node> <node> <node> <special NAME="thisone"></special> </node> <node> <special>dont want this one</special> </node> </node> </node>
(节点可以包含节点…)@H_502_3@
我需要找到第一个基于它的属性名为“NAME”和值“thisone”.@H_502_3@
那么我需要它的父(节点).@H_502_3@
我试过这个:@H_502_3@
specialItems = tempXML.*.(hasOwnProperty(“NAME”));@H_502_3@
但似乎没有做任何事情.@H_502_3@
??@H_502_3@
谢谢!@H_502_3@
解决方法
通常在ActionScript中,您将使用E4X而不是XPath.你想要的就是这样实现的:
var xml:XML = <node>...</node>; var selected:XMLList = xml.descendants().(attribute("NAME") == "thisone"); var first:XML = selected[0]; var parent:XML = first.parent();
如果你知道你想要的节点是一个特殊的,那么你可以使用:@H_502_3@
var selected:XMLList = xml..special.(attribute("NAME") == "thisone");
代替.这是一个nice E4X tutorial.@H_502_3@
如果您使用@NAME ==“thisone”语法,那么您需要在所有XML节点上使用NAME属性,但不要使用attribute()运算符语法.@H_502_3@
我在上面添加了parent()调用;您只能在条件中使用子代直接获取父项:@H_502_3@
xml..node.(child("special").attribute("NAME") == "thisone");