Author Topic: FlxXML - load XML data easily  (Read 3993 times)

BrandonIsABadass

  • Member
  • **
  • Posts: 46
  • Karma: +0/-0
    • View Profile
FlxXML - load XML data easily
« on: Thu, Nov 4, 2010 »
So, after searching around a bit for a usable XML class and fumbling around with the as3 XML Loader stuff, I decided to create a wrapper class for the greensock XMLLoader to be used in flixel.

How to use it:
Code: [Select]
//start the load
new FlxXML("http://test.com/path/to/your/xml.xml",callback);

//somewhere else
public function callback(data:XML):void{
    trace("HOLY DATA BATMAN! \n" + data);
    //normal as3 XML manipulation goes here
}

The class:
Code: [Select]
package org.flixel
{
import com.greensock.events.LoaderEvent;
import com.greensock.loading.XMLLoader;

public class FlxXML
{

public var xLoader:XMLLoader;
public var data:XML;
public var url:String = null;
private var callback:Function;

public function FlxXML(url:String, callback:Function = null)
{
this.callback = callback;
this.url = url;

xLoader = new XMLLoader(this.url, {
name:"data",
onComplete:loadComplete,
onProgress:loadProgress,
onError:loadError,
integrateProgress:false
});

xLoader.load();
}

public function loadProgress(e:LoaderEvent):void {
trace("progress: " + Math.floor(e.target.progress*100));
}

public function loadComplete(e:LoaderEvent):void {
trace(e.target + " is complete!");

data = xLoader.getContent("data");

if(callback != null)
callback(data);
}

public function loadError(e:LoaderEvent):void {
trace("error occured with " + e.target + ": " + e.text);
}
}
}

Requires the greensock XMLLoader stuff to be in your classpath.

The callback function can be null if you wanted to just do something but not handle the return xml.. for example pinging a server with data, etc.

DelishusCake

  • délicieux gâteau
  • Member
  • **
  • Posts: 44
  • Karma: +0/-0
    • View Profile
Re: FlxXML - load XML data easily
« Reply #1 on: Sat, Mar 12, 2011 »
Thats pretty sweet :D. However, i don't particularly care for greensock's stuff, and I made my own without greensock.
Code: [Select]
package org.flixel
{
import flash.events.*;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.utils.ByteArray;
/**
* A simple class that loads an XML file.
* @author Redkast Games
*/
public class FlxXML
{
private var loader:URLLoader;
/**
* Create the loader
*/
public function FlxXML() { }
/**
* Load the XML file from an external server.
* @param url The url of the XML file (e.g. http://www.somesite.com/main.xml);
* @param onComplete The function to execute once the load is complete( function c(data:XML):void );
* @param onProgress The optional function to use on progress (function p( loaded:int, total:int):void);
*/
public function loadExternal(url:String, onComplete:Function, onProgress:Function = null):void
{
loader = new URLLoader(new URLRequest(url));
loader.addEventListener(Event.COMPLETE, function complete(e:Event):void
{
onComplete(XML(e.target.data));
});
loader.addEventListener(IOErrorEvent.IO_ERROR, function ioerror(e:IOErrorEvent):void
{
throw new Error("The specified XML file could not be found, load aborted");
});
if (onProgress != null) {
loader.addEventListener(ProgressEvent.PROGRESS, function progress(e:ProgressEvent):void
{
onProgress(e.bytesLoaded, e.bytesTotal);
});
}
}
/**
* Convert an embeded file to XML
* @param xml The embedded XML file.
* @param callback Optional callback function (function c(data:XML):void);
* @return The class as XML;
*/
public function loadEmbedded(xml:Class, callback:Function = null):XML
{
var b:ByteArray = new xml;
var x:XML = XML(b.readUTFBytes(b.length));
if (callback != null) callback(x);
return x;
}
}

}
I also added a function to convert embedded XML to native XML.
« Last Edit: Tue, Mar 15, 2011 by redkast »

sopesope

  • Guest
Re: FlxXML - load XML data easily
« Reply #2 on: Fri, May 20, 2011 »
Hey, redkast, that code looks great, but (shame on me, really) I can't figure out how to call it from another file.  :-[

I have this:
Code: [Select]
public var xml:FlxXML;
[Embed(source="data/testdial01.xml",mimeType="application/octet-stream")] protected var embXML:Class;

And then I try to load the code like this:
Code: [Select]
xml=new FlxXML();
xml.loadEmbedded(embXML);

But all I get is the variable xml yielding a FlxXML class, not an XML file… or so I think. I'm confused.

sopesope

  • Guest
Re: FlxXML - load XML data easily
« Reply #3 on: Fri, May 20, 2011 »
Nevermind, worked it out:
Code: [Select]
[Embed(source="data/testdial01.xml",mimeType="application/octet-stream")]
protected var embXML:Class;
public var xml:FlxXML;
public var xmlTest : XML;

xml=new FlxXML();
xmlTest=xml.loadEmbedded(embXML);