Buscar número de entrada XML
Frecuentes
Visto 216 veces
1
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
1 Respuestas
1
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
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas xml actionscript-3 or haz tu propia pregunta.
Si ejecuto el código rastrear is empty. I guess a number should come up? - Uli
@Uli: I had put
entry
en lugar deENTRY
. Can you try with my updated answer? Make sure that whatever you put in E4X after themyXML
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). - DirkgentlyThis 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 whatchildindex
works on. Can you try changing theLOG_ID
to such that there is only an unique instance found? Otherwise, you will have to save the query result in anXMLList
and iterate over each element and callchildindex
on these. - DirkgentlySorry I don't understand what you mean. Can you show me an example please? - Uli