0

XML File

    <?xml version="1.0" encoding="utf-8"?>


    <dictionary>

        <entries>ABALONE</entries>
        <entries>ABANDON</entries>
        <entries>ABAXIAL</entries>

    </dictionary>

ActionScript 3

var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("myXML.xml"));
myLoader.addEventListener(Event.COMPLETE, OnloadXML);

var wordSearch:String = "ABAXIAL";

function OnloadXML(e:Event):void{

     myXML = new XML(e.target.data);

     function checkWord(searchWord:String){
         //if "searchWord" exist in the xml, then return true else return false.
     }

}

I tried for-loop but the words are just too many and it makes the graphics processing a bit laggy

I'm new to XML and ActionScript

thanks for the help.

2
  • Try var wordSearch:String = "ABAXIAL"; since it is a String. Commented Jul 31, 2014 at 15:25
  • I typed it wrong here. But actually it's var wordSearch:String = "ABAXIAL"; What I mean is the function to be done. Commented Jul 31, 2014 at 15:35

2 Answers 2

1

While you can iterate over each XML node and look for the string, it is likely (untested assumption) faster and easier to simply look for the word in the XML string data (prior to parsing as XML)

var wordSearch:String = "ABAXIAL";

function OnloadXML(e:Event):void{

     var xString:String = e.target.data;

     myXML = new XML(e.target.data);

     function checkWord(searchWord:String){
         return xString.indexOf(searchWord) > -1;
     }

}
Sign up to request clarification or add additional context in comments.

Comments

0

This is how I would do it. Just a modification of your code, but it loops over each word in your XML file, and compares it against the word you are looking for. If it exists, it reports back true; otherwise, false just as you requested.

var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("myXML.xml"));
myLoader.addEventListener(Event.COMPLETE, OnloadXML);

var wordSearch:String = "ABAXIAL";

function OnloadXML(e:Event):void{

    myXML = new XML(e.target.data);
    var wordList:XMLList = myXML.entries;
    var wordExistenceStatus:Boolean = checkWord(wordList);
    trace(wordExistenceStatus);
}

function checkWord(wordList:XMLList){
    //if "searchWord" exist in the xml, then return true else return false.
    var word:XML;
    for each(word in wordList) {
        if( wordSearch == word.toString()) {
            return(true);
        }
    }
    return(false);
}

I've run this code on Flash CS6 and it works properly.

4 Comments

thank you so much. It really works fast. BTW, I have 5 xml which are the same structure but the length of the words differ from 3 to 7. If I need to change my xml checklist, all I have to change is the wordList variable right?
If you have multiple XML files you wish to search, I would create an array of filenames, then loop over each filename where you load that xml file and everything else remains the same.
Thanks so much but wouldn't it slow a bit because it'll be searching in all of the xml files. I was thinking of another way like loading all xml files then change the wordList variable corresponding to the length of the word. That way, only 1 xml file will be searched.
You'll have to run tests to optimize your searching; however, you won't know until you try each method. Changing the wordList variable doesn't mean anything because if your data is still in different XML files, you still have to load in multiple XMl files at some point. If you combine all your xml files into a single one, it will be a little faster, how much faster, I don't' know, as it depends on the size of the files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.