Bobby Francis Joseph
Bobby Francis Joseph

Reputation: 600

Finding the position of occurring of a node whose attributes values we have in AS3

I have an XML

 <p id="section02_01"/>
 <p id="section02_02"/>
 <p id="section02_03"/>
 <p id="section02_04"/>
 <p id="section02_05"/>
 <p id="section02_06"/>
 <p id="section02_07"/>
     <p id="section02_08"/>

I have the value of id as section02_05. How can I find the position of corresponding p node using XML or E4X. Assume that the root node for this.

Upvotes: 1

Views: 779

Answers (2)

Patrick
Patrick

Reputation: 15717

If have understand your question, you can use the method childIndex

var xml:XML=<xml>
<p id="section02_01"/>
 <p id="section02_02"/>
 <p id="section02_03"/>
 <p id="section02_04"/>
 <p id="section02_05"/>
 <p id="section02_06"/>
 <p id="section02_07"/>
 <p id="section02_08"/>
</xml>

trace(xml.p.(@id=="section02_05").childIndex())

Upvotes: 3

jhocking
jhocking

Reputation: 5577

Well first off you can parse for that node using an attribute filter as described here: http://www.kirupa.com/developer/flashcs3/using_xml_as3_pg6.htm

Then once you have that node you can find it's position in the XML tree by using parent()n: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/XMLList.html#parent%28%29

Write a recursive function that both goes to the node's parent and keeps a count of how many times it has recursed*, and then it stops recursing* when parent() returns "undefined".

*Is this a real word? I hope you get what I mean.

Upvotes: 0

Related Questions