在网上找了很多资料,大都是根据HTTPService获取,但是不能进行遍历,只能通过
<mx:Model id="m" source="data/data1.xml"/>
<mx:ArrayCollection id="ac" source="{ArrayUtil.toArray(m.result)}"/>
才可以遍历,下面是完整的代码。
首先是外部的xml文件,data/data1.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<data>
<result date="11.01.11">
<news>270</news>
<radio>0</radio>
<forum>0</forum>
<blog>80</blog>
<sns>80</sns>
<question>1</question>
</result>
<result date="11.01.12">
<news>190</news>
<radio>10</radio>
<forum>10</forum>
<blog>20</blog>
<sns>20</sns>
<question>11</question>
</result>
<result date="11.01.13">
<news>185</news>
<radio>20</radio>
<forum>2</forum>
<blog>10</blog>
<sns>22</sns>
<question>3</question>
</result>
<result date="11.01.14">
<news>300</news>
<radio>12</radio>
<forum>200</forum>
<blog>15</blog>
<sns>30</sns>
<question>15</question>
</result>
<result date="11.01.15">
<news>310</news>
<radio>0</radio>
<forum>14</forum>
<blog>18</blog>
<sns>30</sns>
<question>18</question>
</result>
<result date="11.01.16">
<news>360</news>
<radio>14</radio>
<forum>3</forum>
<blog>10</blog>
<sns>11</sns>
<question>20</question>
</result>
<result date="11.01.17">
<news>290</news>
<radio>14</radio>
<forum>6</forum>
<blog>20</blog>
<sns>29</sns>
<question>9</question>
</result>
<result date="11.01.18">
<news>365</news>
<radio>0</radio>
<forum>9</forum>
<blog>2</blog>
<sns>30</sns>
<question>7</question>
</result>
<result date="11.01.19">
<news>280</news>
<radio>5</radio>
<forum>0</forum>
<blog>5</blog>
<sns>25</sns>
<question>10</question>
</result>
<result date="11.01.20">
<news>0</news>
<radio>270</radio>
<forum>15</forum>
<blog>30</blog>
<sns>22</sns>
<question>2</question>
</result>
<result date="11.01.21">
<news>320</news>
<radio>90</radio>
<forum>2</forum>
<blog>20</blog>
<sns>28</sns>
<question>13</question>
</result>
</data>
下面mxml文件:
<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
verticalAlign="middle"
backgroundColor="white"
initialize="initValue();"
width="700"
height="410">
<mx:Script>
<![CDATA[
import mx.utils.ArrayUtil;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
[Bindable]
public var myData:ArrayCollection;
[Bindable]
public var max:Number = 0;
private function initValue():void{
max = ac.getItemAt(0)["news"];
for(var i:int=0;i<ac.length;i++){
for(var j:String in ac.getItemAt(i)){
//如果不是Number类型则不作比较
if(!ac.getItemAt(i)[j] is Number){
continue;
}
//遍历json对象
if(ac.getItemAt(i)[j] > max){
max = ac.getItemAt(i)[j];
}
}
}
}
]]>
</mx:Script>
<mx:SolidColor id="sc1" color="blue" alpha=".3"/>
<mx:SolidColor id="sc2" color="red" alpha=".3"/>
<mx:SolidColor id="sc3" color="green" alpha=".3"/>
<mx:SolidColor id="sc4" color="yellow" alpha=".3"/>
<mx:SolidColor id="sc5" color="purple" alpha=".3"/>
<mx:SolidColor id="sc6" color="black" alpha=".3"/>
<mx:Stroke id = "s1" color="blue" weight="2"/>
<mx:Stroke id = "s2" color="red" weight="2"/>
<mx:Stroke id = "s3" color="green" weight="2"/>
<mx:Stroke id = "s4" color="yellow" weight="2"/>
<mx:Stroke id = "s5" color="purple" weight="2"/>
<mx:Stroke id = "s6" color="black" weight="2"/>
<mx:Model id="m" source="data/data1.xml"/>
<mx:ArrayCollection id="ac" source="{ArrayUtil.toArray(m.result)}"/>
<mx:Panel title="网站类型指数"
height="100%" width="100%" layout="horizontal">
<mx:Label text="{'最大值是:'+max}"/>
<mx:Legend dataProvider="{lineChart}"
markerHeight="5" markerWidth="15"/>
<mx:LineChart id="lineChart" height="100%" width="100%"
paddingLeft="5" paddingRight="5"
showDataTips="true" dataProvider="{ac}">
<mx:horizontalAxis>
<mx:CategoryAxis categoryField="date"/>
</mx:horizontalAxis>
<mx:series>
<mx:LineSeries yField="news" form="curve" displayName="新闻" lineStroke="{s1}" />
<mx:LineSeries yField="radio" form="curve" displayName="视频" lineStroke="{s2}"/>
<mx:LineSeries yField="forum" form="curve" displayName="论坛" lineStroke="{s3}"/>
<mx:LineSeries yField="blog" form="curve" displayName="博客" lineStroke="{s4}" />
<mx:LineSeries yField="sns" form="curve" displayName="SNS" lineStroke="{s5}"/>
<mx:LineSeries yField="question" form="curve" displayName="问答" lineStroke="{s6}"/>
</mx:series>
</mx:LineChart>
</mx:Panel>
</mx:Application>
就可以成功的找出配置文件中的最大值。