我想使用ActionScript 3检查字符串是否在我的XML文件中

时间:2014-07-31 15:23:49

标签: xml actionscript-3 flash

XML文件

    <?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.
     }

}

我试过循环,但是这些单词太多了,这使得图形处理有点滞后

我是XML和ActionScript的新手

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

虽然您可以迭代每个XML节点并查找字符串,但很可能(未经测试的假设)更快更容易在XML字符串数据中查找单词(在解析为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;
     }

}

答案 1 :(得分:0)

我就是这样做的。只需对代码进行修改,但它会遍历XML文件中的每个单词,并将其与您要查找的单词进行比较。如果存在,则报告true;否则,false正如您所要求的那样。

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);
}

我已在Flash CS6上运行此代码,但它可以正常运行。