admin管理员组

文章数量:1432573

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!

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!

Share Improve this question asked May 7, 2013 at 4:28 RamRam 1,0844 gold badges16 silver badges26 bronze badges 2
  • Possible duplicate: stackoverflow./questions/1773550/… – Dan Commented May 7, 2013 at 4:31
  • possible duplicate of how to convert xml to json using jquery – shyam Commented May 7, 2013 at 4:33
Add a ment  | 

2 Answers 2

Reset to default 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);

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

本文标签: Convert XML to Object using jQuery of plain javascriptStack Overflow