how to find specific xml data by attribute name/value in flex / actionscript
From some xml I want to find items that have a specific attribute and value.
Here is example xml:
<node>
<node>
<node>
<special NAME="thisone"></special>
</node>
<node>
<special>dont want this one</special>
</node>
</node>
</node>
(nodes can contain nodes...)
I need to find the first based on it has an attribute named "NAME" and value of "thisone".
then I need its parent (node).
I tried this:
specialItems = tempXML.*.开发者_运维知识库(hasOwnProperty("NAME"));
but didnt seem to do anything.
??
Thanks!
In ActionScript you'll use E4X rather than XPath, generally. What you want can be achieved like this:
var xml:XML = <node>...</node>;
var selected:XMLList = xml.descendants().(attribute("NAME") == "thisone");
var first:XML = selected[0];
var parent:XML = first.parent();
If you know the node you want is a special
, then you can use:
var selected:XMLList = xml..special.(attribute("NAME") == "thisone");
instead. Here's a nice E4X tutorial.
If you use the @NAME == "thisone"
syntax, then you do need the NAME attribute on all of your XML nodes, but not if you use the attribute()
operator syntax instead.
I added the parent()
call above; you could get the parent directly by using the child only in the conditional:
xml..node.(child("special").attribute("NAME") == "thisone");
You could do this in 2 ways:
- add the NAME attribute to all your special nodes, so you can use an E4X conditions(xml)
- use a loop to go through special nodes and check if there is actually a NAME attribute(xml2)
Here is an example:
//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|');
}
}
There is a pretty good and easy to follow article on E4X on the yahoo flash developer page.
精彩评论