xml裝成json格式

來源:互聯網
上載者:User

標籤:xml   json   javascript   

function xmlToJson(xml) {      // Create the return object      var obj = {};         if (xml.nodeType == 1) { // element          // do attributes          if (xml.attributes.length > 0) {              for (var j = 0; j < xml.attributes.length; j++) {                  var attribute = xml.attributes.item(j);                  obj[attribute.nodeName] = attribute.nodeValue;              }          }      } else if (xml.nodeType == 3) { // text          obj = xml.nodeValue;      }      // do children      if (xml.hasChildNodes()) {          for(var i = 0; i < xml.childNodes.length; i++) {              var item = xml.childNodes.item(i);              var nodeName = item.nodeName;              if (typeof(obj[nodeName]) == "undefined") {                if(nodeName=='#text'){                  obj=xmlToJson(item);                }else{                  obj[nodeName] = xmlToJson(item);                }              } else {                  if (typeof(obj[nodeName].length) == "undefined") {                      var old = obj[nodeName];                      obj[nodeName] = [];                      obj[nodeName].push(old);                  }                  obj[nodeName].push(xmlToJson(item));              }          }      }      return obj;};

eg:

a.xml如下:

<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom">    <title type="text">部落格園</title>    <id>uuid:2c932236-94ae-434b-a607-29900040a1fd;id=8360</id>    <updated >2014-12-04T08:25:04Z</updated>    <link href="http://www.cnblogs.com/"/>    <entry>        <id>4143069</id>        <title type="text">這道SQL筆試題你會怎麼寫</title>        <summary type="text">最近面試了一些的候選人,行業經驗三年到七年不等,起初覺得這個的無需準備筆試題,礙於領導執念,就在真實項目中提取道題目,這裡僅單說其中一道難度中等偏下的題目,拋開面試不談,單看筆試的話幾輪下來答題情況並不理想,至今沒有發現有人能寫出邏輯滴水不漏又效能最大化的指令碼,難</summary>        <published>2014-12-04T15:54:00+08:00</published>        <updated>2014-12-04T08:25:04Z</updated>        <author>            <name>Xpivot</name>            <uri>http://www.cnblogs.com/xpivot/</uri>            <avatar/>        </author>        <link rel="alternate" href="http://www.cnblogs.com/xpivot/p/4143069.html"/>        <blogapp>xpivot</blogapp>        <diggs>1</diggs>        <views>260</views>        <comments>11</comments>    </entry>    <entry>        <id>4133547</id>        <title type="text">s2sh三大架構整合過程(僅供參考)</title>        <summary type="text">三大架構顧名思義就是非常有名的,,,架構整合的方法很多,現在我寫一個非常簡單的整合過程,相信大家一看就會!這裡使用的、、第一步,搭建環境、匯入</summary>        <published>2014-12-04T15:13:00+08:00</published>        <updated>2014-12-04T08:25:04Z</updated>        <author>            <name>瞿成鋒部落格園</name>            <uri>http://www.cnblogs.com/quchengfeng/</uri>            <avatar/>        </author>        <link rel="alternate" href="http://www.cnblogs.com/quchengfeng/p/4133547.html"/>        <blogapp>quchengfeng</blogapp>        <diggs>0</diggs>        <views>117</views>        <comments>0</comments>    </entry></feed>
轉化

if (window.XMLHttpRequest){    xmlhttp=new XMLHttpRequest(); }else{    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}xmlhttp.open("GET",'a.xml',false);xmlhttp.send();xmlDoc=xmlhttp.responseXML;console.log(JSON.stringify(xmlToJson(xmlDoc)))

結果如下

{  "feed": {    "xmlns": "http://www.w3.org/2005/Atom",     "title": "部落格園",     "id": "uuid:2c932236-94ae-434b-a607-29900040a1fd;id=8360",     "updated": "2014-12-04T08:25:04Z",     "link": {      "href": "http://www.cnblogs.com/"    },     "entry": [      {        "id": "4143069",         "title": "這道SQL筆試題你會怎麼寫",         "summary": "最近面試了一些的候選人,行業經驗三年到七年不等,起初覺得這個的無需準備筆試題,礙於領導執念,就在真實項目中提取道題目,這裡僅單說其中一道難度中等偏下的題目,拋開面試不談,單看筆試的話幾輪下來答題情況並不理想,至今沒有發現有人能寫出邏輯滴水不漏又效能最大化的指令碼,難",         "published": "2014-12-04T15:54:00+08:00",         "updated": "2014-12-04T08:25:04Z",         "author": {          "name": "Xpivot",           "uri": "http://www.cnblogs.com/xpivot/",           "avatar": { }        },         "link": {          "rel": "alternate",           "href": "http://www.cnblogs.com/xpivot/p/4143069.html"        },         "blogapp": "xpivot",         "diggs": "1",         "views": "260",         "comments": "11"      },       {        "id": "4133547",         "title": "s2sh三大架構整合過程(僅供參考)",         "summary": "三大架構顧名思義就是非常有名的,,,架構整合的方法很多,現在我寫一個非常簡單的整合過程,相信大家一看就會!這裡使用的、、第一步,搭建環境、匯入",         "published": "2014-12-04T15:13:00+08:00",         "updated": "2014-12-04T08:25:04Z",         "author": {          "name": "瞿成鋒部落格園",           "uri": "http://www.cnblogs.com/quchengfeng/",           "avatar": { }        },         "link": {          "rel": "alternate",           "href": "http://www.cnblogs.com/quchengfeng/p/4133547.html"        },         "blogapp": "quchengfeng",         "diggs": "0",         "views": "117",         "comments": "0"      }    ]  }}


xml裝成json格式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.