Convert XML to Object using jQuery of plain javascript - Stack Overflow

Here is a sample XML:<xml id="javascriptObject"><name>Joe<name><age>

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745589867a4634767.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信