最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin1浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论