Hi,
another option for typed data is the serializer from the xflas2 package.
It serializes/deserializes classes as well.
Or example imagine you not only have strings and numbers in your xml but a
type MyClass as well.
After deserializing it, you immediately calls methods on your object.
The reason I love that, is because instead of manually translating a model
to xml and back, i simply say Serializer.serialize (myModel) and when i get
back to it, i deserialize it, and its immediately available to add listeners
to for example.
Still in alpha though:)
greetz
JC
On 11/16/06, Julien Vignali <[EMAIL PROTECTED]> wrote:
Thanks Rich ;-)
You don't have compiler errors because you're assigning values to your
singleton by using the square brackets [ ] method. By doing this, at
compile
time the compiler doesn't know which property you're trying to modify...
The
property can be of any type or can even not exist at all, the compiler
won't
bother! ;-)
To solve the problem you can use JSON as Eka previously said, or you can
modify your Settings class and make your xml processing aware of Settings'
property types.
Here is a example:
class com.mysite.model.Settings
{
private static var instance:MySettings;
public var custom_color_3:String;
public var background_image:Boolean;
public var background_type:String;
public var background_alpha:Number;
public var title_mode:String;
/*
* Private constructor
*/
private function MySettings(){
init();
}
/**
* Set the defaults values
* (otherwise properties are seen as 'undefined')
*/
private function init():Void {
custom_color_3 = "";
background_alpha = 0;
background_image = false;
background_type = "";
title_mode = "";
}
// rest of class..
}
function parseSettings(success:Boolean){
var settingsBranch:XMLNode = app_xml.firstChild;
var name:String; // will store the current node name
var value:String; // will store the current node string value
var type:String; // will store Setting's property type
var settingNodes:Array = settingsBranch.childNodes;
// iterate through setting nodes
for(var i:Number = 0; i < settingNodes.length; i++){
var settingNode:XMLNode = settingNodes[i];
name = settingNode.nodeName;
value = settingNode.firstChild.nodeValue;
type = typeof(settings[name]);
switch(type){
case "number": settings[name] = Number(value); break;
case "boolean": settings[name] = (value == "1") ? true : false;
break;
default: settings[name]= value; break;
}
}
}
Hope it helps,
Julien
2006/11/16, Rich Rodecker < [EMAIL PROTECTED]>:
> julien:
>
> sure here's a sample:
>
> class com.mysite.model.Settings {
>
> private static var instance = null;
>
> public var custom_color_3:String;
> public var background_image:Boolean;
> public var background_type:String;
> public var background_alpha:Number;
> public var title_mode:String;
>
> //rest of class....
> }
>
> Here is the function where I parse the xml into the properties above,
pretty
> simple:
>
> function parseSettings(){
>
> var settingsBranch = app_xml.firstChild.firstChild;
> var c = settingsBranch.childNodes;
>
> var num = settingsBranch.childNodes.length;
>
> var nextBranch:XMLNode = settingsBranch.childNodes [0];
>
> while(nextBranch){
> var tagName = nextBranch.nodeName;
> settings[tagName] = nextBranch.firstChild.nodeValue;
> nextBranch = nextBranch.nextSibling;
> }
>
> }
>
>
> and here is what the xml looks like:
> <settings>
> <custom_color_3>#FFFFFF</custom_color_3>
> <background_image>1</background_image>
> <background_type>jpg</background_type>
> <background_alpha>100</background_alpha>
> <title_mode>PROFILE</title_mode>
> </settings>
>
>
>
> eka: - yeah, i know xml is only a string..my problem is that i am
assigning
> a string to properties that are clearly typed as something other than a
> string, yet flash isnt complaining.
>
>
>
>
> On 11/16/06, eka <[EMAIL PROTECTED]> wrote:
> >
> > Hello :)
> >
> > a XML is only a String .... all properties (nodeValue) or attributes
are
> > strings... you must transform the type of your values with customs
> > "deserialize" functions.
> >
> > For me it's better to use JSON for example : http://www.json.org/ (you
> > keep
> > the primitive types of your objects with the serialize/deserialize)
> >
> > You can try to use my openSource framework "VEGAS" who implement JSON
and
> > EDEN (the best solution to text format datas)
> >
> > Install VEGAS : http://vegas.riaforge.org/ (use the SVN or the zip
link
in
> > this page to download my framework)
> >
> > Test the JSON examples in VEGAS in the vegas.string package :
> > http://svn.riaforge.org/vegas/AS2/trunk/bin/test/vegas/string/
> >
> > Test the Eden examples (Burrrn library used in VEGAS) :
> > http://svn.riaforge.org/vegas/AS2/trunk/bin/test/buRRRn/eden/
> >
> > Test JSON and Eden extension in ASGard (extension of VEGAS) :
> > http://svn.riaforge.org/vegas/AS2/trunk/bin/test/asgard/net/
(JSONLoader,
> > EdenLoader)
> >
> > Test my example of Config pattern and localization pattern in
> > http://svn.riaforge.org/vegas/AS2/trunk/bin/test/asgard/config/ (and
> > bin/test/system)
> >
> > For me ... XML is slow and don't keep the typing !
> >
> > EKA+ :)
> >
> >
> > 2006/11/16, Julien Vignali < [EMAIL PROTECTED]>:
> > >
> > > Hi Rich, could you provide some sample code of your Settings class
and
> > > your
> > > xml processing ?
> > >
> > > 2006/11/16, Rich Rodecker < [EMAIL PROTECTED]>:
> > > >
> > > > Hello,
> > > >
> > > > I've got a class named 'Settings' which has a number of
> > > properties. Those
> > > > properties are strongly typed to various types...string, number,
> > > boolean,
> > > > etc.
> > > >
> > > > I'm loading in xml in a separate Model class, and then parsing the
xml
> > > and
> > > > assigning the that various values in the xml to the properties of
the
> > > > Settings class. However, all the values are being set as a string
> > (that
> > > > part I expected since I am assigning the values of text nodes,
which
> > of
> > > > course are strings, in the xml to the Setting's properties)...and
I'm
> > > not
> > > > getting any parse errors when I try and assign a string to a
property
> > > that
> > > > is strongly typed as a number, or boolean.
> > > >
> > > > I figure since I am trying to use myBranch.firstChild.nodeValuethat
> > > > flash,
> > > > at compile time, can see I am trying to set a string to a property
> > typed
> > > > as
> > > > a Number, so what's going on?
> > > > _______________________________________________
> > > > [email protected]
> > > > To change your subscription options or search the archive:
> > > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> > > >
> > > > Brought to you by Fig Leaf Software
> > > > Premier Authorized Adobe Consulting and Training
> > > > http://www.figleaf.com
> > > > http://training.figleaf.com
> > > >
> > > _______________________________________________
> > > [email protected]
> > > To change your subscription options or search the archive:
> > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> > >
> > > Brought to you by Fig Leaf Software
> > > Premier Authorized Adobe Consulting and Training
> > > http://www.figleaf.com
> > > http://training.figleaf.com
> > >
> > _______________________________________________
> > [email protected]
> > To change your subscription options or search the archive:
> > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
> >
> > Brought to you by Fig Leaf Software
> > Premier Authorized Adobe Consulting and Training
> > http://www.figleaf.com
> > http://training.figleaf.com
> >
> _______________________________________________
> [email protected]
> To change your subscription options or search the archive:
> http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
>
> Brought to you by Fig Leaf Software
> Premier Authorized Adobe Consulting and Training
> http://www.figleaf.com
> http://training.figleaf.com
>
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com
_______________________________________________
[email protected]
To change your subscription options or search the archive:
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Brought to you by Fig Leaf Software
Premier Authorized Adobe Consulting and Training
http://www.figleaf.com
http://training.figleaf.com