[Pixlib] XMLToObject
Francis Bourre
peterphonix at usa.net
Thu Nov 17 11:32:15 EST 2005
>Here the code in revision 19, for each node in the root node you get object's name with name attribute or nodename if no name attribute. If name already exist you get one unique name based on current name.Then you get type from attributes but if there's no type you define it as xml2o.
true :)
>And if it's an xml2o type the data is set with the entire node, else you use nodeValue (witch return null if nodes only contains subnodes) so you can't browse for children node...
I can't catch your point here :)
"xml2o" uses recursivity to build objects.
what's the problem?
Here is a quick list of my unit tests, all is working as expected for me.
/*
* tests
*/
public function testNumber() : Void
{
assertTrue("", (_o.cost == 13 && typeof(_o.cost) == "number") );
}
public function testString() : Void
{
assertTrue("", (_o.title == "pixLib" && typeof(_o.title) == "string") );
}
public function testArray() : Void
{
var a:Array = _o.liste;
assertTrue("", _o.liste, ( a[0] == 'allo, tu es là ?' &&
a[1] == "l'avion" &&
a[2] == "c'est moi, huhu !" &&
a[3] == 'hahah' &&
typeof(a) == "array") );
}
public function testCode() : Void
{
assertTrue("", (_o.code == "<i>testons</i>" && typeof(_o.code) == "string") );
}
public function testBoolean() : Void
{
assertTrue("", (_o.visible && typeof(_o.visible) == "boolean") );
}
public function testNameAttribute() : Void
{
assertTrue("", (_o.blog == "http://www.tweenpix.net/blog" && typeof(_o.blog) == "string") );
}
public function testClonedNodes() : Void
{
var n:Number = 0;
for (var p : String in _o) if (_o[p] == "film0" || _o[p] == "film1") n++;
assertEquals("", n, 2);
}
public function testRecusrsiveParsing() : Void
{
assertEquals("", _o.dvds.film, "Lost Highway");
}
public function testCustomType() : Void
{
var p:Point = new Point(1, 2);
assertTrue("", p.equals(_o.vecteur));
}
public function testClassInstance() : Void
{
var e:EventType = new EventType("onTest");
assertTrue("", _o.event == "onTest" && _o.event instanceof EventType);
}
<fr>
<cost type="number">13</cost>
<title type="string">pixLib</title>
<liste type="array">'allo, tu es là ?', "l'avion", "c'est moi, huhu !", 'hahah'</liste>
<code type = "string"><![CDATA[<i>testons</i>]]></code>
<visible type = "boolean">true</visible>
<url type="string" name="blog">http://www.tweenpix.net/blog</url>
<film type="string">film0</film>
<film type="string">film1</film>
<dvds>
<film type="string">Lost Highway</film>
</dvds>
<vecteur type="point">1, 2</vecteur>
<event type="class">"com.bourre.events.EventType", "onTest"</event>
</fr>
francis
----- Original Message -----
From: Cédric Néhémie
To: Pixlib at osflash.org
Sent: Thursday, November 17, 2005 3:01 PM
Subject: Re: [Pixlib] XMLToObject
Tell me if i'm wrong :
Here the code in revision 19, for each node in the root node you get object's name with name attribute or nodename if no name attribute. If name already exist you get one unique name based on current name.
Then you get type from attributes but if there's no type you define it as xml2o. And if it's an xml2o type the data is set with the entire node, else you use nodeValue (witch return null if nodes only contains subnodes) so you can't browse for children node...
public function deserializeData(oXML:XMLNode, oL) : Object
{
var o:Object = new Object();
for (var x = 0; x < oXML.childNodes.length; x++)
{
var sName:String = oXML.childNodes[x].attributes.name;
if (sName == undefined) sName = oXML.childNodes[x].nodeName;
if ( o[sName] )
{
Logger.LOG( "XMLToObject parsed two nodes with identical names : '" + sName + "'.", LogLevel.WARN);
sName = _getUniquePropName(o, sName);
}
o[sName] = {};
var sType:String = oXML.childNodes[x].attributes.type;
if (sType == undefined) sType = "xml2o";
o[sName]["varType"] = sType;
o[sName]["data"] = (sType != "xml2o") ? oXML.childNodes[x].firstChild.nodeValue : oXML.childNodes[x];
}
for(var p:String in o) _oDeserializer.deserialize(oL, p, o[p].varType, o[p].data, this);
return oL;
}
Francis Bourre a écrit :
Hello Cedric!
"xml2o" is an old behavior which was available long time ago before
deserializer's introduction in rev14.
It's been deprecated, but I kept the old code implementation behind the
scenes to keep some compatibility with old projects.
Important: You don't need any "xml2o" attribute to get Object type now. ;)
Any node nesting some other nodes will be automatically casted as an Object.
About "type" attribute, it's there coz in my design choice, node names are
automatically transformed to properties names.
In a future version of XMLToObject, (pixLib for AS3), there will be no need
of "type" attributes. I use ECMAScript notation to cast the good type.
Hope that helps,
francis
----- Original Message -----
From: "Cédric Néhémie" <cartel.com at free.fr>
To: "Mailling PixLib" <Pixlib at osflash.org>
Sent: Thursday, November 17, 2005 9:12 AM
Subject: [Pixlib] XMLToObject
Hi Francis,
Something seems strange in XMLToObject.deserializeData, the line :
o[sName]["data"] = (sType != "xml2o") ?
oXML.childNodes[x].firstChild.nodeValue : oXML.childNodes[x];
if type of the node is not xml2o you use nodeValue to set the data
field. I think that is not to XMLToObject to choose witch of nodeValue
or childNodes use to fill data. The XMLToObjectDeserializer is more
approriate to that task, and that let us choosing xml structure.
Another strange thing is that you have preferred use a "type" attribute
to switch deserializer's behaviour instead of nodename, why have you
decided to chose this alternative?
Regards
Cedric
------------------------------------------------------------------------------
_______________________________________________
Pixlib mailing list
Pixlib at osflash.org
http://osflash.org/mailman/listinfo/pixlib_osflash.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://osflash.org/pipermail/pixlib_osflash.org/attachments/20051117/863a6bed/attachment.htm
More information about the Pixlib
mailing list