-1

我有一个 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 将导出“名词”

任何人请帮助我...

4

1 回答 1

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 加载xml 过滤的 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;

}

这是一个工作版本

我希望这有帮助!

于 2013-05-19T07:51:29.223 回答