Uli
Uli

Reputation: 2693

Find XML entry number

I need to get the node number of an entry but I only have the LOG_ID. How find that number out?

<LOG>
<ENTRY LOG_ID="01042012"/>
<ENTRY LOG_ID="03052012"/>
<ENTRY LOG_ID="09052012"/>
</LOG>

Thanks. Uli

Upvotes: 1

Views: 223

Answers (1)

dirkgently
dirkgently

Reputation: 111130

Use E4X processing as described here and the getting started documentation:

var myXML:XML = 
 <LOG>
  <ENTRY LOG_ID="01042012"/>
  <ENTRY LOG_ID="03052012"/>
  <ENTRY LOG_ID="09052012"/>
 </LOG>

trace( myXML.ENTRY.(@LOG_ID==09052012).childIndex() ); /* retrieve entire node */

You can also store a reference to this node in an XML object:

 var index:int = myXML.ENTRY.(@LOG_ID==09052012).childIndex();

Note: The childindex function (and a few others) work on individual nodes. However, if your input example has multiple nodes with the same attribute value you're using to retrieve you will get a list of nodes (i.e. an XMLList) instead of a single node. Now, in order to find out the indices of such children you will need to do the following:

for each ( var selectedNode in myXML.ENTRY.(@LOG_ID==09052012) )
    trace( selectedNode.childIndex() );

You can always check if your E4X query returned a list via the following:

var candidates:XMLList = myXML.ENTRY.(@LOG_ID==09052012) as XMLList;
if (candidates != null) { // a list 
        // do something ...
}

Upvotes: 1

Related Questions