How to read data from XML file in flex?
Use URLLoader
var ldr:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("file.xml");
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(request);
private function onLoad(e:Event):void
{
var ldr:URLLoader = URLLoader(e.target);
trace(ldr.data);//traces the string content of file
var myxml:XML = new XML(ldr.data);
trace(myxml.toXMLString());
}
You can load the xml files using the URLRequest and URLLoader and then process them. Check following example, flex - load xml using URLLoader and extract data
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="bookdat.send()">
mx:HTTPService id="bookdat" url="books.xml" resultFormat="e4x"
result="bookhandler(event)"/>
<mx:DataGrid id="dg" dataProvider="{booklist}" width="500"/>
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;
[Bindable]
var booklist:XMLList=new XMLList();
public function bookhandler(e:ResultEvent)
{
booklist=e.result.stock.(category=="Fiction").name;
// booklist=e.result.stock
}
]]>
</mx:Script>
</mx:WindowedApplication>
Check out the HTTPService example in Tour de Flex.
Related
I'm looking for an example of an AIR application, that all it does it loads Firefox. Can someone point me to an example that can be modified to do this, or that does this exactly?
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script><![CDATA[
import flash.net.URLRequest;
public function clickButton():void{
var request : URLRequest = new URLRequest('C:\\path to mozilla\firefox.exe');
navigateToURL(request )
}
]]></mx:Script>
<mx:Button click="clickButton()" label="Launch Firefox"/>
</mx:WindowedApplication>
if not sure about location or on mac
use properties like this to get to default folders..
var appDir:File = File.applicationDirectory;
var appStoreDir:File= File.applicationStorageDirectory;
var desktopDir:File = File.desktopDirectory;
var docDir:File = File.documentsDirectory;
var userDir:File = File.userDirectory;
var rootDirArr:Array = File.getRootDirectories();
We have requirement with the AIR application which loads the flex generated swf which inturn loads the flash generated swf using SWFLoader. This is not working as desired. This gives the following error:
SecurityError: Error #3226: Cannot import a SWF file when LoaderContext.allowCodeImport is false.
This is our AIR application.
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
<mx:Script>
<![CDATA[
import mx.controls.SWFLoader;
[Embed(source="FlexLoadingFlash.swf")]
public var flexMovie:Class;
private function initApp():void {
// First convert the Swf into MovieClip
var movieclip:MovieClip = new flexMovie();
// get the byteArray from movieClip
var byteArray:ByteArray = movieclip.movieClipData;
var swfLoader:SWFLoader = new SWFLoader();
// load bytearray into swfLoader
swfLoader.source = byteArray;
swfLoader.maintainAspectRatio = false;
swfLoader.percentHeight = vbox.height;
swfLoader.percentWidth = vbox.width;
swfLoader.invalidateDisplayList();
swfLoader.invalidateSize();
// now add the swfloader into container
vbox.addChild(swfLoader);
}
]]>
</mx:Script>
<mx:VBox id="vbox" width="100%" height="100%" verticalCenter="0" horizontalCenter="0" cacheAsBitmap="true" >
</mx:VBox>
</mx:WindowedApplication>
Please let me know how can we fix this issue.
Use Loader.loadBytes() to load your SWF. Create an instance of LoaderContext. loadBytes method takes a LoaderContext instance as a parameter. Set the allowCodeImport property of your LoaderContext instance to true and it should work
Or you can just add these three lines before you set the source
var loaderContext: LoaderContext = new LoaderContext();
loaderContext.allowLoadBytesCodeExecution = true;
swfLoader.loaderContext = loaderContext;
<mx:SWFLoader id="swfObj"
width="100%" height="100%"
complete="swfObj_completeHandler(event)"/>
<fx:Script>
<![CDATA[
[Bindable]
[Embed(source="assets/soundbar.swf")]
private static var swfClass:Class;
private var swfSoundBar : MovieClip;
[Bindable] private var mp3Player:MP3Player = MP3Player.getInstance();
protected function init(event:FlexEvent):void
{
swfSoundBar = new swfClass();
var byteArray:ByteArray = swfSoundBar.movieClipData;
var loaderContext: LoaderContext = new LoaderContext();
loaderContext.allowLoadBytesCodeExecution = true;
swfObj.loaderContext = loaderContext;
swfObj.source = byteArray;
}
protected function swfObj_completeHandler(event:Event):void
{
swfSoundBar = SWFLoader(event.target).content as MovieClip;
swfSoundBar.width = 32;
swfSoundBar.height = 14;
swfSoundBarShowHide();
}
protected function swfSoundBarShowHide():void
{
if (swfSoundBar){
if (mp3Player.isPlaying){
swfSoundBar.gotoAndStop(0);
swfSoundBar.stop();
} else {
swfSoundBar.gotoAndPlay(0);
}
}
}
]]>
</fx:Script>
I have this item renderer MyRenderer.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:HBox xmlns:mx="http://www.adobe.com/2006/mxml" implements="mx.core.IDataRenderer" >
<mx:Script>
<![CDATA[
[Bindable]
public var greylist : XML;
public function newInstance() : * {
return new MyRenderer();
}
]]>
</mx:Script>
</mx:HBox>
and try to append the item renderer to my datagridcolumn by a classfactory
myRenderer = ClassFactory(MyRenderer);
myRenderer.properties = { greylist: this.greylist };
Now when debugging I get the error that MyRenderer cannot be converted into classfactory.
Can someone please help me?
Thanks in advance
Sebastian
Try this instead:
myRenderer = new ClassFactory(MyRenderer);
I wish to pass many small PNG files as base64 encoded URIs within an XML response, but there seems to be no way to make flex present these images. I was thinking of the data uri scheme, but it appears not to be supported.
Proposed solutions
Use Loader.LoadBytes
Tried it and it doesn't seem to work (none of the events are triggered).
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="1276" height="849" creationComplete="drawImage()">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.utils.Base64Decoder;
private function loaderCompleteHandler(event:Event):void {
Alert.show("loader done");
}
private function errorHandler(e:IOErrorEvent):void {
Alert.show("error" + e.toString());
}
public function drawImage() : void
{
var b64png : String = "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IAAAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1JREFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jqch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0vr4MkhoXe0rZigAAAABJRU5ErkJggg==";
var l : Loader = new Loader();
var decoder : Base64Decoder = new Base64Decoder();
decoder.decode(b64png);
var bytes : ByteArray = decoder.flush();
l.addEventListener(Event.COMPLETE, loaderCompleteHandler);
l.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
l.loadBytes(bytes);
}
]]>
</mx:Script>
<mx:Image x="10" y="10" width="155" height="118" id="image1"/>
</mx:Application>
Can someone please tell me what I did wrong?
If you decode the image data into a ByteArray then you can use Loader.loadBytes(byteArray) to load it as an image.
You could use something this to load the image:
var deco64:Base64Decoder = new Base64Decoder;
deco64.decode("base64StringWithTheImageData");
var arrBytes:ByteArray = deco64.toByteArray();
img.load(arrBytes);
Hope this helps!
You can assign the byte array returned from the decoder directly to the image's source property.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()">
<mx:Script>
<![CDATA[
import mx.utils.Base64Decoder;
private function init():void {
var b64png : String = "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAABGdBTUEAALGPC/xhBQAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB9YGARc5KB0XV+IAAAAddEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIFRoZSBHSU1Q72QlbgAAAF1JREFUGNO9zL0NglAAxPEfdLTs4BZM4DIO4C7OwQg2JoQ9LE1exdlYvBBeZ7jqch9//q1uH4TLzw4d6+ErXMMcXuHWxId3KOETnnXXV6MJpcq2MLaI97CER3N0vr4MkhoXe0rZigAAAABJRU5ErkJggg==";
var decoder : Base64Decoder = new Base64Decoder();
decoder.decode(b64png);
var bytes : ByteArray = decoder.flush();
img.source = bytes;
}
]]>
</mx:Script>
<mx:Image id="img" />
</mx:Application>
Is it possible to embed a Vimeo video in Flex?
When I try to embed this link in a <mx:SWFLoader/> , it doesn't come up as anything.
The best I could do was this. Using the Vimeo Api to show img and linking img to video.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="black" >
<mx:Script>
<![CDATA[
import flash.net.navigateToURL;
import flash.net.URLRequest;
private var xml:XML = new XML();
private var loadXML:URLLoader = new URLLoader();
private function carregar(string:String):void{
//carregando XML adicionando o evento COMPLETE
loadXML.load(new URLRequest("http://vimeo.com/api/clip/"+string+".xml"));
loadXML.addEventListener(Event.COMPLETE, lista);
}
//função Listar
private function lista(event:Event):void {
xml = new XML(event.target.data);
img.source = xml.clip.thumbnail_large;
img.addEventListener(MouseEvent.CLICK, abrir);
}
private function abrir(event:MouseEvent):void{
var req:String = "http://vimeo.com/moogaloop.swf?clip_id="+xml.clip.clip_id;
var request:URLRequest = new URLRequest(req);
navigateToURL(request,"_blank");
}
]]>
</mx:Script>
<mx:TextInput x="209" y="55" width="182" id="codVimeo"/>
<mx:Button x="398" y="55" label="Carregar" click="carregar(codVimeo.text)"/>
<mx:Image x="113" y="94" width="361" height="318" id="img"/>
<mx:Label x="113" y="57" text="Código Vimeo:" color="#FFFFFF"/>
</mx:Application>