How to structure an xml file for the XMLToObjectDeserializer

Author: Sebastien Jouhans

original thread: http://groups.google.co.uk/group/lowra-support/browse_thread/thread/f7090eb98ac849bb

The XMLToObjectDeserializer class is a very useful class; it will parse your XML and put it into a given object. When used properly it will also store the data in its given type which will save you the task to cast it when retrieving it.

Here is a xml file example (the type attribute defines the variable type):

<config>
  <car>
    <name type="String">golf</name>
    <colour type="Number">0xFF0000</colour>
    <extras type="Array">leather,rims,sun roof,fog light</extras>
  </car>
  <car>
    <name type="String">polo</name>
    <colour type="Number">0xx00FF00</colour>
    <extras type="Array">smoked window,rims,abs</extras>
  </car>
</config>

The code to deserialise that xml is the following:

var config : Object = new Object();
var _oDeserializer : XMLLoaderDeserializer = new XMLToObjectDeserializer();
//the xml variable stores the data of the xml file
//config is the object used to store the deserialised data from the xml file
_oDeserializer.deserialize( config , xml );

Since all the data was stored in an object it is then very easy to access it. Note that if more than 2 nodes have the same name they will be put into an array.

For instance retrieving the name of the 1st car would be

 config.car[0].name.value;

retrieving the name of the 2nd colour would:

 config.car[1].colour.value;

And the beauty is since you have defined the variables type the parser has casted all the data for you.

Discussion

Enter your comment
 
 
projects/lowra/faq/xmltoobjectdeserializer.txt · Last modified: 2008/03/02 07:06 (external edit)
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki