標籤: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格式