在flex中读取XML文件中的数据

时间:2010-05-31 06:50:38

标签: flex

如何从flex中读取XML文件中的数据?

4 个答案:

答案 0 :(得分:2)

使用URLLoader

var ldr:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("file.xml");
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(request);

private function onLoad(e:Event):void
{
  var ldr:URLLoader = URLLoader(e.target);
  trace(ldr.data);//traces the string content of file
  var myxml:XML = new XML(ldr.data);
  trace(myxml.toXMLString());
}

答案 1 :(得分:1)

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

<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="bookdat.send()">

mx:HTTPService id="bookdat" url="books.xml" resultFormat="e4x" 

result="bookhandler(event)"/>




<mx:DataGrid id="dg" dataProvider="{booklist}" width="500"/>

<mx:Script>
    <![CDATA[

        import mx.rpc.events.ResultEvent;

       [Bindable]
        var booklist:XMLList=new XMLList();
        public function bookhandler(e:ResultEvent)
        {
      booklist=e.result.stock.(category=="Fiction").name;
     // booklist=e.result.stock
        }
    ]]>
</mx:Script>
</mx:WindowedApplication>

答案 2 :(得分:1)

您可以使用URLRequest和URLLoader加载xml文件,然后处理它们。请查看以下示例flex - load xml using URLLoader and extract data

答案 3 :(得分:0)