Xml to json format

Source: Internet
Author: User

Xml to json format

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 is as follows:

 
 

  Blog
  
   
Uuid: 2c932236-94ae-434b-a607-29900040a1fd; id = 8360
  
  
   
2014-12-04T08: 25: 04Z
  
  
  

   
    
4143069
   
   How do you write this SQL pen question?
   I recently interviewed some candidates from three to seven years of experience in the industry. At first, I felt that I didn't need to prepare a pen question. As a result, I extracted a question from a real project, here we only talk about one of the most difficult questions. Aside from the interview, it is not ideal to answer questions in a few rounds after a single test. So far, no script can be written to maximize the performance of the logic, hard
   
    
2014-12-04T15: 54: 00 + 08: 00
   
   
    
2014-12-04T08: 25: 04Z
   
   
    
Xpivot
   
   
    
Http://www.cnblogs.com/xpivot/
   
   
   
    
Xpivot
   
   
    
1
   
   
    
260
   
   
    
11
   
  
  

   
    
4133547
   
   S2sh integration process (for reference only)
   As the name suggests, the three frameworks are very famous. There are many methods to integrate the frameworks. Now I am writing a very simple integration process. I believe you will see it at a glance! Here, the first step is to set up the environment and import
   
    
2014-12-04T15: 13: 00 + 08: 00
   
   
    
2014-12-04T08: 25: 04Z
   
   
    
Qu chengfeng blog Park
   
   
    
Http://www.cnblogs.com/quchengfeng/
   
   
   
    
Quchengfeng
   
   
    
0
   
   
    
117
   
   
    
0
   
  
 
Conversion

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)))

The result is as follows:

{"Feed": {"xmlns": "http://www.w3.org/2005/Atom", "title": "blog", "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": "How do you write this SQL pen question?", "summary": "I have recently interviewed some candidates, and my industry experience may vary from three to seven years, at first, I felt that I didn't need to prepare a pen question. Because the leaders were obsessed with it, I extracted a question from the real project. Here I will only talk about one of the difficult questions. I will not talk about the interview, it is not ideal to answer questions in a few rounds after a single test. No one has been able to write logic without leaking performance. It is difficult to maximize scripts. "," published ":" 2014-12-04T15: 54: 00 + 08: 00 "," updated ":" 2014-12-04T08: 25: 04Z "," author ": {"name": "xnate", "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 three major framework integration processes (for reference only)", "summary": "three boxes As the name suggests, the architecture is very famous. There are many methods to integrate the framework. Now I am writing a very simple integration process. I believe you will see it! Here, the first step is to set up the environment and import "," published ":" 2014-12-04T15: 13: 00 + 08: 00 "," updated ":" 2014-12-04T08: 25: 04Z "," author ": {" name ":" Qu Cheng Feng blog "," 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"}]}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.