首页 > 代码库 > xml装成json格式

xml装成json格式

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.mamicode.com/"http://www.cnblogs.com/"/>>转化

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格式