5

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.com/pub/javascript/to_xml/trunk/to_xml.js, but does somebody has proved experience and could recommend any specific library?

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. Commented Jan 14, 2009 at 8:43

2 Answers 2

2

Try this:

Converting Between XML and JSON by Stefan Goessner

Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.