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.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","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","author": {
          "name": "瞿成锋博客园","uri": "http://www.cnblogs.com/quchengfeng/","href": "http://www.cnblogs.com/quchengfeng/p/4133547.html"
        },"blogapp": "quchengfeng","diggs": "0","views": "117","comments": "0"
      }
    ]
  }
}
原文链接:https://www.f2er.com/xml/297572.html

猜你在找的XML相关文章