Buscar número de entrada XML

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>

Gracias. Uli

preguntado el 21 de mayo de 12 a las 18:05

1 Respuestas

Use E4X processing as described aquí y documentación inicial:

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 :

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

Nota la 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 ...
}

contestado el 21 de mayo de 12 a las 21:05

Si ejecuto el código rastrear is empty. I guess a number should come up? - Uli

@Uli: I had put entry en lugar de ENTRY. Can you try with my updated answer? Make sure that whatever you put in E4X after the myXML variable name matches in case the actual entry/entries in your XML file. Also note that E4X typically skips the root node (i.e. LOG en tu caso). - Dirkgently

This comes up: Error #1086: The childIndex method only works on lists containing one item. - Uli

@Uli: So, your E4X query returns an XMLList instead of a single node which is what childindex works on. Can you try changing the LOG_ID to such that there is only an unique instance found? Otherwise, you will have to save the query result in an XMLList and iterate over each element and call childindex on these. - Dirkgently

Sorry I don't understand what you mean. Can you show me an example please? - Uli

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.