xml数据:
- <?xml version="1.0" encoding="UTF-8"?>
- <shuju>
- <shuiguo month="Jan-04">
- <apple>81768</apple>
- <orange>60310</orange>
- <banana>43357</banana>
- </shuiguo>
- <shuiguo month="Feb-04">
- <apple>81156</apple>
- <orange>58883</orange>
- <banana>49280</banana>
- </shuiguo>
- </shuju>
<?xml version="1.0" encoding="UTF-8"?> <shuju> <shuiguo month="Jan-04"> <apple>81768</apple> <orange>60310</orange> <banana>43357</banana> </shuiguo> <shuiguo month="Feb-04"> <apple>81156</apple> <orange>58883</orange> <banana>49280</banana> </shuiguo> </shuju>
程序代码:
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="http.send()">
- <mx:Script>
- <![CDATA[
- import mx.rpc.events.ResultEvent;
- import mx.collections.ArrayCollection;
- [Bindable]
- public var myData:ArrayCollection;
- public function resultHandler(event:ResultEvent):void{
- error.text = event.result as String;
- myData = new ArrayCollection(event.result.shuju.shuiguo);
- }
- ]]>
- </mx:Script>
- <!--<mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="resultHandler(event)"/>-->
- <mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="myData=ArrayCollection(http.lastResult.shuju.shuiguo)"/>
- <mx:Panel title="Line Chart" width="465" height="496">
- <mx:LineChart id="chart" dataProvider="{myData}">
- <mx:horizontalAxis>
- <mx:CategoryAxis categoryField="month"/>
- </mx:horizontalAxis>
- <mx:series>
- <mx:LineSeries yField="apple" name="Apple"/>
- <mx:LineSeries yField="orange" name="Orange"/>
- <mx:LineSeries yField="banana" name="Banana"/>
- </mx:series>
- </mx:LineChart>
- <mx:TextArea width="393" id="error"/>
- </mx:Panel>
- </mx:Application>
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="http.send()"> <mx:Script> <![CDATA[ import mx.rpc.events.ResultEvent; import mx.collections.ArrayCollection; [Bindable] public var myData:ArrayCollection; public function resultHandler(event:ResultEvent):void{ error.text = event.result as String; myData = new ArrayCollection(event.result.shuju.shuiguo); } ]]> </mx:Script> <!--<mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="resultHandler(event)"/>--> <mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="myData=ArrayCollection(http.lastResult.shuju.shuiguo)"/> <mx:Panel title="Line Chart" width="465" height="496"> <mx:LineChart id="chart" dataProvider="{myData}"> <mx:horizontalAxis> <mx:CategoryAxis categoryField="month"/> </mx:horizontalAxis> <mx:series> <mx:LineSeries yField="apple" name="Apple"/> <mx:LineSeries yField="orange" name="Orange"/> <mx:LineSeries yField="banana" name="Banana"/> </mx:series> </mx:LineChart> <mx:TextArea width="393" id="error"/> </mx:Panel> </mx:Application>
现在有一个问题我不明白:
我把那个httpservice改写成:
- <mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="resultHandler(event)"/>
<mx:HTTPService id="http" url="http://localhost:8080/XML2ArrayCollection/data/data.xml" useProxy="false" result="resultHandler(event)"/>
然后添加了脚本:
- public function resultHandler(event:ResultEvent):void{
- error.text = event.result as String;
- myData = new ArrayCollection(event.result.shuju.shuiguo);
- }
public function resultHandler(event:ResultEvent):void{
error.text = event.result as String;
myData = new ArrayCollection(event.result.shuju.shuiguo);
}
这样就不行了,我测试了下,好像event.result里面没有内容,没有返回xml内容吗?谁大牛帮忙解释下。
Reference:
1. http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=charts_intro_108_12.html
2. http://hillelcoren.com/2008/09/21/flex-tip-convert-xml-to-arraycollection/
3. http://dreamweaverforum.info/flex/20292-converting-xml-file-array-collection.html