I am looking for a best proved way to serialize JavaScript objects to XML, that could be sent to server further in Ajax style.
Just googling I've found some options like .js, but does somebody has proved experience and could remend any specific library?
I am looking for a best proved way to serialize JavaScript objects to XML, that could be sent to server further in Ajax style.
Just googling I've found some options like http://svn.mirekrusin./pub/javascript/to_xml/trunk/to_xml.js, but does somebody has proved experience and could remend any specific library?
Share Improve this question edited Jan 14, 2009 at 8:33 Gennady Shumakher asked Jan 14, 2009 at 8:06 Gennady ShumakherGennady Shumakher 5,75612 gold badges42 silver badges46 bronze badges 1- is there a reason for not using JSON? It is (a) native to javascript and (b) very lightweight, and (c) has parsers/encoder libraries for just about every language out there. – Ry Biesemeyer Commented Jan 14, 2009 at 8:43
2 Answers
Reset to default 2Try this:
Converting Between XML and JSON by Stefan Goessner
I dont know if there are fameworks that will do this for you but...
// Define class constructor
var SampleObject1 = function()
{
this.name = 'MySampleObject';
this.id = 1;
this.seed = 1.009;
this.createdAt = new Date();
this.obj = null;
};
// Create instance of serializer
var serializer = new Ant.Serializer();
// Register SampleObject1, so serializer gets to know how to deal with such objects
serializer.register('SampleObject1', SampleObject1);
// Create data that will be serialized
var object = new SampleObject1();
object.obj = new SampleObject1();
// Serialize and get string representation
var xml = serializer.save(object).toString();
// Displays (formatting is changed):
// <SampleObject1>
// <name type="string">MySampleObject</name>
// <id type="number">1</id>
// <seed type="number">1.009</seed>
// <createdAt>
// <Date value="2007-7-26T20:31:24.156"/>
// </createdAt>
// <obj>
// <SampleObject1>
// <name type="string">MySampleObject</name>
// <id type="number">1</id>
// <seed type="number">1.009</seed>
// <createdAt>
// <Date value="2007-7-26T20:31:24.156"/>
// </createdAt>
// <obj/>
// </SampleObject1>
// </obj>
// </SampleObject1>
WScript.echo(xml);
// Displays: MySampleObject
WScript.echo(serializer.load(xml).name);