Skip to content

Instantly share code, notes, and snippets.

@rafaelrinaldi
Created March 23, 2011 19:37
Show Gist options
  • Save rafaelrinaldi/883783 to your computer and use it in GitHub Desktop.
Save rafaelrinaldi/883783 to your computer and use it in GitHub Desktop.
A test showing how to use embedded XML files in ActionScript.
<?xml version="1.0" encoding="UTF-8"?>
<document>
<foo>This is my document</foo>
</document>
package {
import flash.utils.ByteArray;
import flash.display.Sprite;
/**
*
* @author Rafael Rinaldi (rafaelrinaldi.com)
* @since 23/02/2011
*
* */
public class EmbeddedXMLTest extends Sprite {
[Embed(source="document.xml", mimeType="application/octet-stream")]
protected const document : Class;
public function EmbeddedXMLTest() {
trace(xml(document));
/*
<document>
<foo>This is my document</foo>
</document>
*/
}
public function xml( p_class : Class ) : XML {
const byteArray : ByteArray = new p_class as ByteArray; // Parsing the XML data as ByteArray
return new XML(byteArray.readUTFBytes(byteArray.length));
}
}
}
package tea.util
{
import flash.utils.ByteArray;
/**
*
* Simple parser for embedded XML files.
*
* @param p_klass Document class.
* @return A XML instance based on document's bytes.
*
* @author Rafael Rinaldi (rafaelrinaldi.com)
* @since 23/03/2011
*
* */
public function parseEmbeddedXML( p_klass : Class ) : XML
{
const bytes : ByteArray = new p_klass as ByteArray;
return new XML(bytes.readUTFBytes(bytes.length));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment