flash10 flash.xml.XMLList by example

There are several approaches to XML in flash9+ HaXe, this approach utilises some of the e4X player built in methods.
Other approaches are 'Fast' the generic Haxe language approach, Xpath a haxe lib and also some optimised classes for flash 9 that can be found on the mailing list.

Since XML can be boring and you want something you can play with and explore, I have included some flash 10 built in 3D and some Perlin Noise, also text embedding.

For the moment I am going to code dump all the classes and compiler files for you to explore. Later I will come back and add more explanations.

info.xml file

<?xml version="1.0" encoding="UTF-8"?>
<info>
    <locations>
        <pos x = '300' y = '250' z = '50'/>
        <pos x = '400' y = '200' z = '100'/>
        <pos x = '500' y = '300' z = '45'/>
        <pos x = '200' y = '280' z = '60'/>
        <pos x = '100' y = '320' z = '0'/>
        <pos x = '100' y = '250' z = '50'/>
        <pos x = '200' y = '200' z = '100'/>
        <pos x = '400' y = '300' z = '45'/>
        <pos x = '500' y = '280' z = '60'/>
        <pos x = '300' y = '320' z = '0'/>
    </locations>
</info>

// Simple extendable XML loader
// author: JLM at Justinfront
 
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.xml.XML;
import flash.xml.XMLList;

typedef XMLloaderEvents = Event;

class XMLloader extends EventDispatcher
{
    
    public var xml:             XML;
    private var urlLoader:      URLLoader;
    private var getting:        Bool;
    
    public function new():Void
    {
        super();
        getting = false;
        init();
    }
    
    
    private function init():Void
    {
        
        //add any functionality here!
        
    }
    
    
    // loadfile is used to load an xml file.
    public function loadfile( file: String ):Void
    {
        
        getting                     = true;
        var urlRequest:URLRequest   = new URLRequest( file );
        urlLoader                   = new URLLoader();
        
        urlLoader.addEventListener( Event.COMPLETE, completeListener );
        urlLoader.load( urlRequest );
        
    }
    
    
    // is called when the file is loaded
    private function completeListener( e: Event ):Void
    {
        
        getting = false;
        xml = new XML( urlLoader.data );
        parseXML();
        // dispatches when xml can be used.
        dispatchEvent( e );
        
    }
    
    
    public function parseXML():Void
    {
        
        trace('parsing!!');
        // code to parse xml
        
    }
    
    
}

// Main class with xml parser and simple 3d setup
// author: JLM at Justinfront

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.Lib;
import flash.events.Event;
import flash.events.EventDispatcher;

import XMLloader;
import PerlinText;

import flash.xml.XML;
import flash.xml.XMLList;

import flash.geom.Matrix3D;
import flash.geom.Vector3D;
import flash.geom.Point;

class XmlLoadingbyexample
{
    
    
    public static function main(): Void { new XmlLoadingbyexample(); }
    
    private var _stage:         MovieClip;
    private var _view_mc:       Sprite;
    private var _xmlLoader:     XMLloader;
    private var _3dHolder:      Sprite;
    private var _centre:        Point;
    
    
    public function new()
    {
        
        _stage                              = Lib.current;
        
        basic3D();
        
        _xmlLoader                          = new XMLloader( );
        _xmlLoader.addEventListener( XMLloaderEvents.COMPLETE, xmlloaded );
        _xmlLoader.loadfile('info.xml' );
        
    }
    
    
    private function xmlloaded( e: Event ):Void
    {
        
        var info:       XML                 = _xmlLoader.xml;
        var listxml:    List<Dynamic>       = new List();
        var xmllist:    XMLList             = info.locations.child('*');
        var xmlline:    XML;
        var attributes: XMLList;
        var obj:        Dynamic;
        var param:      Dynamic;// XML and Int
        var prop:       String;
        
        for( str in Reflect.fields( xmllist ) )
        {
            
            xmlline         = Reflect.field( xmllist, str );
            attributes      = xmlline.attribute('*');
            obj             = new PerlinText( _3dHolder, 'HaXe', 0, 0, 0 );
            
            for( no in Reflect.fields( attributes ) )
            {
                
                param   = Reflect.field( attributes, no );
                prop    =  'set_' + param.name();
                
                if( Reflect.hasField( obj, prop ) ) 
                {
                    
                    if( prop == 'set_x' ){ param -= _centre.x; }
                    if( prop == 'set_y' ){ param -= _centre.y; }
                    if( prop == 'set_z' ){ param -= 50; }
                    
                    Reflect.callMethod( obj, Reflect.field( obj, prop ), [ param ] );
                    
                }
                
                
            }
            
            listxml.add( obj );
            
        }
        
        _stage.addEventListener( Event.ENTER_FRAME, rotate );
        
    }
    
    
    private function rotate( e: Event ):Void
    {
        
        _3dHolder.rotationY += 3;
        
    }
    
    
    private function basic3D():Void
    {
        
        _centre = new Point(400, 200);
        _stage.transform.perspectiveProjection.projectionCenter = _centre;
        
        _3dHolder = new Sprite();
        _3dHolder.x = _centre.x;
        _3dHolder.y = _centre.y;
        _stage.addChild( _3dHolder );
        
    }
    
    
}

the swfmill file lib.swfmill that defines the embedded font ( Gentium a opensource font easily sourced on the web)

<?xml version="1.0" encoding="iso-8859-1" ?>
<movie version="9">
    <background color="#555555"/>
    <library>
        <font name="Gentium" import=" *!!! Put you path to the font here !!!*/Library/Fonts/GenR102.TTF" 
glyphs="HaXe"/>
    </library>
    <frame>
    </frame>
</movie>
// Fun class that creates embeded text with Perlin Noise Coloring
// author: JLM at Justinfront

import flash.geom.Point;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.ColorTransform;
import flash.geom.Transform;
import flash.text.TextField;
import flash.text.Font;
import flash.text.TextFormat;
import flash.text.TextFieldAutoSize;
import flash.text.AntiAliasType;

class Gentium extends flash.text.Font {}

class PerlinText
{
    
    
    private var _parent:        Sprite;
    private var _x:             Float;
    private var _y:             Float;
    private var _z:             Float;
    private var _text:          String;
    
    private var _mc:            Sprite;
    private var _bg:            Sprite;
    private var _masc:          Sprite;
    private var _tx:            TextField;
    
    
    
    public function set_x( val: Float ): Float
    {
        
        _mc.x     = val;
        _bg.x     = val;
        _masc.x   = val;
        _x        = val;
        return      val;
        
    }
    
    public function get_x(): Float { return _x; }
    
    
    public function set_y( val: Float ): Float
    {
        
        _mc.y       = val;
         _bg.y     = val;
        _masc.y     = val;
        _y          = val;
        return        val;
        
    }
    
    public function get_y(): Float { return _y; }
    
    
    public function set_z( val: Float ): Float
    {
        
        _mc.z       = val;
         _bg.z     = val;
        _masc.z     = val;
        _z          = val;
        return        val;
        
    }
    
    public function get_z(): Float { return _z; }
    
    
    public function new(    
                            parent_:    Sprite,
                            text_:      String,
                            x_:         Float, 
                            y_:         Float,
                            z_:         Float
                         ): Void
    {
        
        _parent = parent_;
        _text   = text_;
        _x      = x_;
        _y      = y_;
        _z      = z_;
        
        addPerlinNoiseTxt();
        
    }
    
    private function addPerlinNoiseTxt(): Void
    {
        
        _mc                                 = new Sprite();
        _bg                                 = new Sprite();
        _bg.x                               = _x;
        _bg.y                               = _y;
        _bg.z                               = _z;
        _mc.x                               = _x;
        _mc.y                               = _y;
        _mc.z                               = _z;
        
        var wid:        Int                 = 130;
        var hi:         Int                 = 60;
        var bd:         BitmapData          = new BitmapData( wid, hi, false, 0);
        var randomNum:  Int                 = Math.floor( Math.random() * 10 );
        var x:          Int                 = 0;
        
        var colorTrans: ColorTransform      = new ColorTransform();
        
        colorTrans.redOffset                = 256 * 2 * Math.random() - 256;
        colorTrans.blueOffset               = 256 * 2 * Math.random() - 256;
        colorTrans.greenOffset              = 256 * 2 * Math.random() - 256;
        
        var colorTrans2: ColorTransform      = new ColorTransform();
        
        colorTrans2.redOffset                = 256 * 2 * Math.random() - 256;
        colorTrans2.blueOffset               = 256 * 2 * Math.random() - 256;
        colorTrans2.greenOffset              = 256 * 2 * Math.random() - 256;
        
        var trans:      Transform           = new Transform( _mc );
        var trans2:     Transform           = new Transform( _bg );
        
        trans.colorTransform                = colorTrans;
        trans2.colorTransform                = colorTrans2;
        
        // someting like perlin noise you adjust parameters till it has a look that is agreeable, unless you really want a wood effect.
        bd.perlinNoise( wid, hi, 6, randomNum, false, false, 1, true, [ new Point( x++, x ), new Point( 50 - x, x ) ] );
        
        var bm:         Bitmap              = new Bitmap( bd );
        _masc                               = new Sprite();
        
        _parent.addChild( _masc );
        
        _tx                                 = new TextField();
        _tx.embedFonts                      = true;
        
        // tracing htmlText from a flash IDE textfield gives you some html in a form the player expects and it can be easier to set up spacing etc..
        _tx.htmlText                        =   '<TEXTFORMAT LEADING="2"><P ALIGN="LEFT">'+
                                                    '<FONT FACE="Gentium" SIZE="50" COLOR="#7FB7FF" LETTERSPACING="3" KERNING="0">'+
                                                        _text +
                                                    '</FONT>'+
                                                '</P></TEXTFORMAT>';
        
        _tx.autoSize                        = TextFieldAutoSize.LEFT;
        _masc.x                             = _x;
        _masc.y                             = _y;
        _masc.z                             = _z;
        _masc.cacheAsBitmap                 = true;
        
        //use text as mask of perlin noise
        _mc.mask                            = _masc;
        
        _mc.addChild(        bm  );
        _bg.addChild(        new Bitmap( bd.clone() )  );
        _bg.alpha = 0.9;
        _parent.addChild(   _bg  );
        _parent.addChild(   _mc  );
        _masc.addChild(     _tx  );
         
    }
    
}

flash 10 haxe compiler file xmlLoadingbyexample.hxml

#build file
-swf-lib lib.swf
-swf xmlLoadingbyexample.swf
-swf-version 10
-main XmlLoadingbyexample
-swf-header 800:600:30:ffffff

version #5533, modified 2009-02-20 05:03:54 by JLM