如何将输入文本与xml进行比较

时间:2013-05-18 10:56:24

标签: xml actionscript-3 flash-cs6

我有一个search_btn,search_txt(输入文本),type_txt(动态文本)和这样的xml文件:

<ArrayOfWord xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <word name="hello " Type="noun"  />
  <word name="hi" Type="verb" />
    <word />
</ArrayOfWord>

当我在search_txt中输入文本时,我想将其与xml进行比较,如果为true,则导出该类型 例如:在search_txt中输入“hello”,单击search_btn,然后type_txt将导出“名词”

任何人都帮助我...

1 个答案:

答案 0 :(得分:0)

XML

首先,你的xml是错误的......

在XML中,您需要具有单独的开始和结束标记。因此

<word name="hello " Type="noun" />

需要更改为

<word name="hello " Type="noun"></word>
  ^^Opening tag                   ^^Closing tag

其次,xml不会忽略空格,因此name="hello "仅在您输入hello并在之后添加空格时才起作用。要解决这个问题,只需删除多余的空间即可。

更正的XML代码应如下所示:

<ArrayOfWord xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <word name="hello" Type="noun"></word>
  <word name="hi" Type="verb"></word>
  <word></word><!--Nothing here?-->
</ArrayOfWord>

AS3

查看xml loadingxml filtering的AS3文档。知道如何从xml文件中获取所需信息是很好的。

//imports
import flash.events.Event; 
import flash.net.URLLoader; 
import flash.events.MouseEvent;

//create a new xml object
var myXML:XML = new XML();
//import xml
//make sure you change the xml file-path here to your own
var XML_URL:String = "sample.xml"; 
var myXMLURL:URLRequest = new URLRequest(XML_URL); 
var myLoader:URLLoader = new URLLoader(myXMLURL); 
//if the xml loads correctly go to a function
myLoader.addEventListener(Event.COMPLETE, xmlLoaded); 

//Function that occurs if xml loads
function xmlLoaded(event:Event):void 
{ 
    //set myXML to the xml data in the file
    myXML = XML(myLoader.data);
}
//When the button is pressed find the type
search_btn.addEventListener(MouseEvent.CLICK,findword);

//function that finds the type
function findword(event:MouseEvent){
        //set search_string to the word to match
    var search_string:String = search_txt.text;
        //set str (string) to the node value "Type" when 
        //the node value "name" is equal to search_string
    var str:String = myXML.word.(@name == search_string ).@Type;
        //put str into the Type text box
    type_txt.text = str;

}

这是一个有效的version

我希望这有帮助!