xml - find out if AS3's XMLList contains string as node value -
is there xmllist equivalent array.indexof?
for example -
var array:array = ['one','two']; trace(array.indexof('two')); // returns 1, since it's @ second position trace(array.indexof('three')); // returns -1, since isn't found
... right? if i've got -
var xml:xml = <list><item>one</item><item>two</item><list> var xmllist:xmllist = list.item;
there's got easier way check see if 1 of nodes in xmllist has particular value looping through of them, right? akin -
xmllist.indexofchildbynodevalue('two'); // returns 1, because it's second child xmllist.indexofchildbynodevalue('three'); // returns -1, because doesn't match children
make sense?
i put demo of how can without looping. though call little more upfront work. though pays off if going checking values lot.
<s:application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minwidth="955" minheight="600" applicationcomplete="_init();"> <fx:declarations> <fx:xml id="movielist" source="http://www.movies.com/rss-feeds/jen-yamato-reviews-rss" /> </fx:declarations> <fx:script> <![cdata[ import mx.logging.ilogger; import mx.logging.log; import mx.logging.logeventlevel; import mx.logging.targets.tracetarget; public static const movie_title:string = "inception"; private var _logger:ilogger = log.getlogger("sandbox"); private function _init() :void { var tracetarget:tracetarget = new tracetarget(); tracetarget.level = logeventlevel.all; log.addtarget(tracetarget); var xmllist:xmllist = movielist..item; var firstitem:xml = movielist..item[0]; var firstitemindex:int = firstitem.childindex(); _logger.info("first item index: " + firstitemindex); var item:xml = xmllist.(title == movie_title)[0]; _logger.info("contains: " + xmllist.contains(item) + " // " + item.childindex()); var actualindex:int = (item.childindex() - firstitemindex); _logger.info("actual index: " + actualindex); _logger.info("result: " + xmllist[actualindex]); } ]]> </fx:script> </s:application>
the basic idea store index of first item
, subtract childindex()
of matched result using e4x search.
Comments
Post a Comment