All I did was search through the examples, so nothing new in
my extract. I just copy and paste and try it out.
maybe this helps. Getting the socket connection to work is a
bit harder, but theres an example for that too.
Note1: this uses an arraycollection, but an xmllistcollection
*might* be better.
Note2: use e4x ( search on it)
////////////////////////////////////////////////////
<?xml version="1.0"?>
<!-- -->
<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"creationComplete="data.send()" >
<mx:HTTPService url="
http://path-to-file.xml" id="data"
resultFormat="e4x"
result="xmlResultHandler(event);"
fault="xmlFaultHandler(event);"
/>
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import flash.net.navigateToURL;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
[Bindable]
private var myxmldata:ArrayCollection = new ArrayCollection;
[Bindable]
private var xmlFeed:XML;
// Result handler for httpservice- gets called after XML is
loaded.
private function xmlResultHandler(event:ResultEvent):void
{
myxmldata.removeAll() ;
xmlFeed = event.result as XML;
for each(var ourxml:XML in xmlFeed.elements() )
myxmldata.addItem(ourxml);
}
// Fault handler for httpservice- displays the error.
private function xmlFaultHandler(event:FaultEvent):void
{
Alert.show(event.fault.message, "Could not load XML file!");
}
// make a looping timer function just for updating the
collection
// USING polling
public function initTimer():void {
// The first parameter in the Timer constructor
// is the interval, in milliseconds.
// The second parameter is how many times to run (0 is
// infinity).
var myTimer:Timer = new Timer(5000, 20);
// Add the listener for the timer event. This connects to
the updateData function
// when an event comes through
myTimer.addEventListener(TimerEvent.TIMER, updateData); //
this is the line that changed from the original
myTimer.start();
}
private function updateData(event:TimerEvent):void {
data.send();
display("Updated based on bowser-based polling...");
}
// ---used to display messages in panel "t"
private function display( text:String ):void {
t.text += text;
t.text += "\n";
}
]]>
</mx:Script>
<mx:Panel title="Column Chart">
<mx:ColumnChart id="myChart" dataProvider="{myxmldata}"
showDataTips="true">
<mx:horizontalAxis>
<mx:CategoryAxis
dataProvider="{myxmldata}"
categoryField="<element1 from xml>"
/>
</mx:horizontalAxis>
<mx:series>
<mx:ColumnSeries
xField="<element1 from xml>"
yField="<element2 from xml>"
displayName="Packets"
/>
<mx:ColumnSeries
xField="<element1 from xml>"
yField="<element3 from xml>"
displayName="BPS"
/>
</mx:series>
</mx:ColumnChart>
<mx:Legend dataProvider="{myChart}"/>
</mx:Panel>
<mx:HBox>
<mx:Button id="b1" label="Update loop based on a timer:
polling" click="initTimer()"/>
<mx:Button id="b2" label="Update Data from file one time
" click="data.send()"/>
</mx:HBox>
<mx:Panel title="Reloading the XML data structure from
file and re-charting the data" height="50%" width="50%"
paddingTop="10" paddingLeft="10" paddingRight="10">
<mx:Text id="t" width="80%">
<mx:text>. </mx:text>
</mx:Text>
</mx:Panel>
</mx:Application>