Reading XML at Run-Time

Posted by Zu on Jul 18, 2007 in Flex |

Много пить вредно, а мало – скучно.

For the Past couple of days i have been trying to find a way to read the XML at Run-Time, and not have to compile and recompile my apps, everytime a little something changes.

So far, there are two methods that do this (that i found).
Though i still need to do some more research on them to find the benefits and uses of each, here they are:

Both of the examaples are just “parts” of my code that are curcial for a successfull execution.

via HTTPServices:
Please note the creationComplete , the imports, HTTPService declaration, and the .lastResult in the dataProvider.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="Quick.send()">
<mx:Script>
  <![CDATA[
 import mx.controls.Alert;
  import mx.rpc.events.FaultEvent;
  import mx.rpc.events.ResultEvent;

[Bindable]
  private var photoFeed:XML;

private function quickResultHandler(event:ResultEvent):void

{
  trace("result handler");
  photoFeed = event.result as XML;
  }

// Fault handler - displays the error
  private function quickFaultHandler(event:FaultEvent):void

{
  trace("fault handler");
  Alert.show(event.fault.message, "Could not load photo feed");
  }

]]>
 </mx:Script>

<mx:HTTPService id="Quick" url="xml/AO_Quick.xml" resultFormat="e4x"
 result="quickResultHandler(event);" fault="quickFaultHandler(event);"/>

<mx:Tree id="myTree" width="100%" height="100%"
  showRoot="false" dataProvider="{Quick.lastResult}" labelField="@title" backgroundAlpha="0.0" borderStyle="none"
  itemClick="executeSearch4()" selectedIndex="0" />   

via XMLLoader

this is a copy-paste exmaple, and should work just fine (providing you have the  XML at hand :)

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
  <mx:Script><![CDATA[
private var myXML:XML;

[Bindable]
  private var myText:String;

private function LoadMyXML():void
  {
  var myXML:XML = new XML();
  var XML_URL:String = "xml/AO_Other.xml";
  var myXMLURL:URLRequest = new URLRequest(XML_URL);
  var myLoader:URLLoader = new URLLoader(myXMLURL);
  myLoader.addEventListener("complete", xmlLoaded);

function xmlLoaded(evtObj:Event): void
  {
  myXML = XML(myLoader.data);
  myText = myXML.toXMLString();
  trace("Data loaded.");
  }
  }

]]></mx:Script>
  <mx:VBox width="100%" height="100%">
  <mx:Button label="Load XML" click="LoadMyXML()"/>
  <mx:TextArea id="idText" text="{myText}" width="100%" height="100%" />
  </mx:VBox>
</mx:Application>
if you have any comments, as to which is better used when, please do leave your input. i'd LOVE it!

Thanks!

Leave a Reply

XHTML: You can use these tags:' <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Copyright © 2007-2010 Xrum! Blog... All rights reserved.
Desk Mess Mirrored v1.5.1 theme from BuyNowShop.com.