Oct 6, 2009 11:12 PM
Loading local XML file in flash builder
-
Like (0)
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?
Put the local file in the bin-debug folder of the project. Then in the Service Wizard specify the filename as url.
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... =/
it may help if you zip up your code and your xml file and attach it as it is difficult for anyone to determine what the problem is without seeing what it is you have done.
David
sry XD here is the file, btw iam using the beta 1
where is this file?! ;-p
the wizard is different from beta1 to beta2
in beta1 you need to enter a sample of the XML/JSON response. copy the contents of the my_file.xml into the text area box. that should get you on your way.
- e
Currently HTTP service wizard is the only option to import xml files. Future releases will have more intutive wizard to import XML files specifically.
thank you guys, i already installed beta 2 and had no problems ^-^
North America
Europe, Middle East and Africa
Asia Pacific
Copyright © 2011 Adobe Systems Incorporated. All rights reserved.
Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy (updated 07-14-2009).