Tips: How to deserialize an xml in object with pixlib

Pixlib is an AS 2.0 framework developed by Francis Bourre.
It’s designed to support event handling, logging, data prĂ©loading, managing sound and video, making transitions, data holders and data structures, patterns implementations… (more infos here).
The svn repository is here to download it.

Working with the childnodes syntax is always constraining. It’s why I prefer to deserialize my xml in object to work more easily.

For example with this xml :

<?xml version="1.0" encoding="UTF-8"?>
<config_flash>
	<lang>fr</lang>
	<country>fr</country>
	<width>990</width>
	<height>690</height>
	<min_width>960</min_width>
	<min_height>590</min_height>
	<flash_version>8</flash_version>
	<use_express_install type="boolean">false</use_express_install>
	<flash_bgcolor><![CDATA[#000000]]></flash_bgcolor>
	<swf_name>loader</swf_name>
	<allow_languages>
		<language c="uk" l="en"/>
		<language c="us" l="en"/>
		<language c="be" l="du"/>
		<language c="fr" l="fr"/>
	</allow_languages>
</config_flash>

With the pixlib, I can simply deserialize it in object like this :

import com.bourre.data.libs.XMLToObject;
import com.bourre.data.libs.XMLToObjectEvent;
import com.bourre.data.libs.XMLToObjectDeserializer;
 
var _pixObj:Object;
var _path:String="thexml.xml";
function _loadXML():Void{
	XMLToObjectDeserializer.PUSHINARRAY_IDENTICAL_NODE_NAMES = true;
        XMLToObjectDeserializer.DESERIALIZE_ATTRIBUTES = true;
	var xto : XMLToObject = new XMLToObject( new Object() );
	xto.setAntiCache( false );
	xto.addEventListener( XMLToObject.onLoadInitEVENT, this, _onXmlDataLoad );
	xto.load( _path );
}
 
function _onXmlDataLoad( e : XMLToObjectEvent ) : Void {
	_pixObj=new Object();
	_pixObj =  e.getObject() ;	
         trace (_pixObj.lang); // fr
         trace(pixObj.min_width); //960
         trace (typeof(_pixObj.use_express_install)); //boolean
	 trace(_pixObj.allow_languages.language[0].c); //uk
	}
 
_loadXML();

As you can see in example, it’s very easy to use those class to deserialize an xml in object.
The first step is to set some static var in the XMLToObjectDeserializer class.

XMLToObjectDeserializer.PUSHINARRAY_IDENTICAL_NODE_NAMES

(default is false) make an array if they are identical node names in your xml. In the example, you can see that they are 4 “language” node and to access them you just have to manipulate the “langage” node as an array.

XMLToObjectDeserializer.DESERIALIZE_ATTRIBUTES

(default is false) deserialize attributes. The node with attributes become an object.

With the attribute “type” you can force the typping of the nodeValue. For example :

 trace (typeof(_pixObj.use_express_install)); //Boolean

Because the node was

<use_express_install type="boolean">false</use_express_install>

but if the node is :

<use_express_install type="string">false</use_express_install>
 trace (typeof(_pixObj.use_express_install)); //String

After setting those vars, create an instance of XMLToObject and load the xml with the “load” method . You can set the anticache to true or false( a timestamp is added to the url of your xml if the anticache is true).
If you want you have some events to listen during the loading of the xml :

XMLToObject.onErrorEVENT
XMLToObject.onLoadInitEVENT
XMLToObject.onLoadProgressEVENT
XMLToObject.onTimeOutEVENT

When the xml is loaded, you just have to push the result in an object and that’s all !


About this entry