Search code examples
xmlactionscript-3air

AS3 - Very large XML makes app freeze


I'm making a dictionary and I have a little problem. My code does what it needs to do (look up the string entered in the XML), but I think I made it the wrong way. Here is my (commented) code, could you tell me if there is a better way (beginner here, so be patient :) ).

First the xml structure:

<dico>
    <word>
        <latin> symbols in latin alphabet</latin>
        <trad> traduction </trad>
        <type> noun, adj, adv, etc. </trad>
        <code> A1D3C1 </code> //what will be looked up
    </word>
<dico>

And the AS3 code:

//the xml file has been imported before
var queryCode:String; //variables used to store the XML data
var queryTranslitt:String;
var queryType:String;
var queryTrad:String;
var intIST:int;

function fnXML(dico: XML): void { //give the var the data taken from XML
    queryCode = dico.mot.code[intIST]
    queryTranslitt = dico.mot.translitteration[intIST];
    queryType = dico.mot.categorie[intIST];
    queryTrad = dico.mot.traduction[intIST];    
}

function fnQuery(e: Event): void { //compare the query variable to the XML

    for (var iSt: int = 0; iSt < 44823; iSt++) { //44823 is the number of
    // definitions in the XML file, that's probably the part that sucks.
        intIST = iSt;
        fnXML(myXML2);
        if (query == queryCode) { //query has been initialized before ofc)
            trace (queryCode);
            trace (queryTranslitt);
            trace (queryType);
            trace (queryTrad);
        }
    }
}

This code need to be executed EVERYTIME the user enters a letter (symbol really) (there are no spaces in the language so a long word can be several words). It works: i click on the first symbol ("i", which corresponds to 3 different words) and I get the traces of those words and their definitions, but the app freezes (i think because of the number of XML nodes it has to go through) Anyone know a better way? Thank you.

PS: By the way, I'm using Flash CC (Air for Android 17.0) if it helps.


Solution

  • It's very easy. Most people don't want to even try to use XML search methods because they don't understand them or don't know how to use them so they all end up with hash tables or loops. null solution would work but would still require a lot of computation to get the hash table set correctly in the first place. Instead do this:

    var codeSearch:String = String(imput_txt.text);//get what's typed
    //xml is your entire xml data
    var xmlresult:XMLList = xml.word.(String(code).indexOf(codeSearch) >= 0)
    //use xml serach method to find any code that contains the searh term
    output_txt.text = "Found " + xmlresult.length() + " results";
    //finally we get a xmlList back with either nothing or some result
    //if xmlresult.length() > 0 then loop through it and do whatever.