ITeye博客

Flex--XML转换成ArrayCollection

yexin218 · 2009-03-28 23:24 · Flex · Flex · XML · Adobe · 脚本 · Apple · Markdown

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"?>
<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)"/>

 然后添加了脚本:

			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

评论(1)

  1. #1 楼 · zhong_pro · 2011-11-18 17:01

    因为要先把event.result转成xml,因为有 转不了,而直接文件则是从略过第一行的 例子如下:

    public var myData:XML=  
                    <tables>  
                        <table>  
                            <name>TRP_USERS</name>  
                            <statement>CREATE TABLE IF NOT EXISTS TRP_USERS (USER_ID INTEGER PRIMARY KEY AUTOINCREMENT,USER_NAME TEXT,PASSWORD TEXT,CREATION_DATE DATE)</statement>  
                        </table>  
                        <table>  
                            <name>TRP_PROJECTS</name>  
                            <statement>CREATE TABLE IF NOT EXISTS TRP_EMPLOYEE_PROJECTS (EMPLOYEE_PROJECT_ID INTEGER PRIMARY KEY AUTOINCREMENT,USER_ID INTEGER,EMPLOYEE_NUMBER TEXT,LEVEL_CODE TEXT,PROJECT_NAME TEXT,PROJECT_MANAGER TEXT,START_DATE DATE,SCHEDULE_END_DATE DATE,TECH_DIRECTOR_NAME TEXT,TECH_LEADER_NAME TEXT,TWO_WEEK_OUT TEXT,MEMO TEXT,UPLOAD_FLAG TEXT,CREATION_DATE DATE)</statement>  
                        </table>  
                    </tables>;  
                private var xmlList:XMLList=new XMLList();  
                private var xmlListCollection:XMLListCollection=new XMLListCollection();  
                private var provider:ArrayCollection;  
                private function init():void  
                {  
                    xmlList=myData.elements("table");  
                    xmlListxmlListCollection.source=xmlList;  
                    provider=new ArrayCollection(xmlListCollection.toArray());  
                    grid.dataProvider=provider;  
                }  
    

评论功能维护中,当前仅展示历史评论。