[英]How do I retrieve the node name from XML in Flex / Actionscript
[英]how to find specific xml data by attribute name/value in flex / actionscript
从某些xml我想找到具有特定属性和值的项目。
这是xml的示例:
<node>
<node>
<node>
<special NAME="thisone"></special>
</node>
<node>
<special>dont want this one</special>
</node>
</node>
</node>
(节点可以包含节点......)
我需要找到第一个基于它的名为“NAME”的属性和值为“thisone”的属性。
然后我需要它的父(节点)。
我试过这个:
specialItems = tempXML。*。(hasOwnProperty(“NAME”));
但似乎没有做任何事情。
??
谢谢!
在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();
如果你知道你想要的节点是special
,那么你可以使用:
var selected:XMLList = xml..special.(attribute("NAME") == "thisone");
代替。 这是一个很好的E4X教程 。
如果使用@NAME == "thisone"
语法,则需要在所有XML节点上使用NAME属性,但如果使用attribute()
运算符语法则不需要。
我在上面添加了parent()
调用; 您可以通过仅在条件中使用子项直接获取父级:
xml..node.(child("special").attribute("NAME") == "thisone");
你可以用两种方式做到这一点:
这是一个例子:
//xml with all special nodes having NAME attribute
var xml:XML = <node>
<node>
<node>
<special NAME="thisone"></special>
</node>
<node>
<special NAME="something else">dont want this one</special>
</node>
</node>
</node>
//xml with some special nodes having NAME attribute
var xml2:XML = <node>
<node>
<node>
<special NAME="thisone"></special>
</node>
<node>
<special>dont want this one</special>
</node>
</node>
</node>
//WITH 4XL conditional
var filteredNodes:XMLList = xml.node.node.special.(@NAME == 'thisone');
trace("E4X conditional: " + filteredNodes.toXMLString());//carefull, it traces 1 xml, not a list, because there only 1 result,otherwise should return
//getting the parent of the matching special node(s)
for each(var filteredNode:XML in filteredNodes)
trace('special node\'s parent is: \n|XML BEGIN|' + filteredNode.parent()+'\n|XML END|');
//WITHOUGH E4X conditional
for each(var special:XML in xml2.node.node.*){
if(special.@NAME.length()){
if(special.@NAME == 'thisone') trace('for each loop: ' + special.toXMLString() + ' \n parent is: \n|XML BEGIN|\n' + special.parent()+'\n|XML END|');
}
}
雅虎Flash开发者页面上的E4X上有一篇非常好且易于理解的文章。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.