2

Here is a sample XML:

<xml id="javascriptObject">
  <name>Joe</name>
  <age>12</age>
  <gender>M</gender>
</xml>

Object produced after digesting the XML above should be equivalent to:

var obj = {name: 'Joe', age: '12', gender: 'M'};

You guys know any functions in javascript or in jQuery that will convert the XML to a javascript object? If none, any ideas on how to do this the best way as possible? Thanks guys!

Ram
  • 1,016
  • 4
  • 15
  • 25
  • Possible duplicate: http://stackoverflow.com/questions/1773550/xml-json-conversion-in-javascript – Dan May 07 '13 at 04:31
  • possible duplicate of [how to convert xml to json using jquery](http://stackoverflow.com/questions/3642789/how-to-convert-xml-to-json-using-jquery) – shyam May 07 '13 at 04:33

2 Answers2

2

Try this, using the parseXML() method:

var xml = '<xml id="javascriptObject"><name>Joe</name><age>12</age><gender>M</gender></xml>',
    xmlDoc = $.parseXML(xml),
    $xml = $(xmlDoc);

var obj = {
    name: $xml.find('name').text(),
    age: $xml.find('age').text(),
    gender: $xml.find('gender').text()
};

console.log(obj);
palaѕн
  • 72,112
  • 17
  • 116
  • 136
0

you can use this project ;) this allows you to convert between json objects and XML objects

Drupars
  • 13
  • 3