Hi, iam almost new to flex and flash builder, i am creating an app that loads a local xml file, but i dont know how can i do this on all the new flash builder interface, what url i have to put on the data services panel in order to acces the file and create the xml data type?
Hey edua91,
There are many many ways to load an xml file into your app. Let's assume you have the following .xml file in data/my_file.xml
<?xml version="1.0" encoding="UTF-8"?>
<people>
<person>
<name>James</name>
<age>24</age>
</person>
<person>
<name>Cynthia</name>
<age>33</age>
</person>
</people>
Here are some methods:
1. (simple mxml)
<fx:Declarations>
<fx:XML id="myXml" source="data/my_file.xml" />
</fx:Declarations>
2. (use HTTPService, as seen here)
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
initialize="myHttpService.send()">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
public var myData:ArrayCollection;
protected function myHttpService_resultHandler(event:ResultEvent):void {
myData = event.result.people.person;
}
]]>
</fx:Script>
<fx:Declarations>
<s:HTTPService id="myHttpService"
url="data/my_file.xml"
result="myHttpService_resultHandler(event)"/>
</fx:Declarations>
</s:Application>
3. (embed in actionscript)
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
initialize="init()">
<fx:Script>
<![CDATA[
[Embed(source=data/my_file.xml, mimeType=application/octet-stream)]
public var MyXMLData:Class;
public var myData:XML;
public function init():void {
var byteArray:ByteArray = new MyXMLData() as ByteArray;
myData = new XML(byteArray.readUTFBytes(byteArray.length));
}
]]>
</fx:Script>
</s:Application>
4. (using data/services panel!!!! what i really wanted to tell you)
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/halo"
xmlns:mylocaldataservice="services.mylocaldataservice.*">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.events.FlexEvent;
import mx.rpc.events.ResultEvent;
public var myData:ArrayCollection;
protected function dataGrid_creationCompleteHandler(event:FlexEvent):void {
getDataResult.token = myLocalDataService.getData();
}
protected function getDataResult_resultHandler(event:ResultEvent):void {
myData = event.result as ArrayCollection;
}
]]>
</fx:Script>
<fx:Declarations>
<s:CallResponder id="getDataResult" result="getDataResult_resultHandler(event)"/>
<mylocaldataservice:MyLocalDataService id="myLocalDataService" fault="Alert.show(event.fault.faultString + '\n' + event.fault.faultDetail)" showBusyCursor="true"/>
</fx:Declarations>
<mx:DataGrid x="260" y="175" id="dataGrid" creationComplete="dataGrid_creationCompleteHandler(event)" dataProvider="{getDataResult.lastResult}">
<mx:columns>
<mx:DataGridColumn headerText="name" dataField="name"/>
<mx:DataGridColumn headerText="age" dataField="age"/>
</mx:columns>
</mx:DataGrid>
</s:Application>
pretty cool, non?
- e
thank you guys but i still have a problem =/ when i want to configure the return type it gives me an error "The url is not valid", what should i do?
i already did what you tell me on the method 4, but when i bind the data it ask me the return type, so on the option i select create a new and name it "person" click next, on that screen i dont know what to do so i click next again and there is where it gives me the error... =/
North America
Europe, Middle East and Africa
Asia Pacific