Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 328
Scott Szretter Avatar asked Feb 09 '10 00:02

Scott Szretter


2 Answers

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");        
like image 52
Michael Brewer-Davis Avatar answered Oct 24 '22 06:10

Michael Brewer-Davis


You could do this in 2 ways:

  1. add the NAME attribute to all your special nodes, so you can use an E4X conditions(xml)
  2. 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([email protected]()){
        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.

like image 39
George Profenza Avatar answered Oct 24 '22 08:10

George Profenza