Question

Comment puis-je obtenir une plage d'éléments de ma xmllist similaire à la méthode slice pour un tableau?

tranche (startIndex, endIndex);

J'essaye quelque chose comme ceci:

            var tempXMLList:XMLList = new XMLList();

            for( var i:int = startIndex; i <= endIndex; i++){
                tempXMLList += originalList[i];
            }

Mais j'obtiens un message d'erreur indiquant que la conversion d'origineList [i]

--- Mettre à jour ---

J'ai utilisé la fonction de Timofei et cela a parfaitement fonctionné.

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    return xmllist.(childIndex() >= startIndex && childIndex() <= endIndex);
}

Cependant, lorsque j'utilise une xmllist qui a déjà été filtrée, elle se brise.

filteredData = filteredData.(team == "Ari");

trace("filteredData.length(): " + filteredData.length().toString());
pData = SliceXMLList(filteredData, startIndex, endIndex);
trace("start: " + startIndex.toString() + " end: " + endIndex.toString());
trace("pdata length: " + pData.length().toString());

sortie

filteredData.length(): 55
start: 0 end: 55
pdata length: 5
Était-ce utile?

La solution

Utilisez e4x.

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    return xmllist.(childIndex() >= startIndex && childIndex() <= endIndex);
}

<₹Update:

Il y a un problème, si vous comptez utiliser cette fonction après un tri e4x, car la fonction childIndex () renvoie les anciennes valeurs des index des nœuds et elle ne peut pas être modifiée.Alors, j'ai une autre idée:

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    for (var i : int = 0; i < xmllist.length(); i++)
        xmllist[i].@realIndex = i;
    xmllist =  xmllist.(@realIndex >= startIndex && @realIndex <= endIndex);
    for (i = 0; i < xmllist.length(); i++)
        delete xmllist[i].@realIndex;
    return xmllist;
}

ou simplement

private function SliceXMLList(xmllist : XMLList, startIndex : int, endIndex : int) : XMLList
{
    var newXMLList : XMLList = new XMLList();
    var currIndex : int = 0;
    for (var i : int = startIndex; i <= endIndex; i++)
        newXMLList[currIndex++] = xmllist[i];
    return newXMLList;
}

C'est la meilleure variante, je pense.Bien sûr, la déclaration e4x sur une ligne est beaucoup plus élégante, mais malheureusement elle n'est pas réutilisable.

Autres conseils

Je ne sais pas comment vous vous attendez à ce que cela fonctionne, mais vous pouvez parcourir tous les enfants, enregistrer chacun d'eux dans un tableau, puis les découper de cette façon

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top