function Xmltojson (XML) {//Create the return object var obj = {}; if (Xml.nodetype = = 1) {//element//Do attributes if (Xml.attributes.length > 0) {fo R (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 as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><feed xmlns= "Http://www.w3.org/2005/Atom" > <title type= "text" > Blog Park </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" > This SQL pen question how would you write </title > <summary type= "text" > recently interviewed a number of candidates, industry experience from three to seven years, initially felt that this need not prepare a pen test, Due to the leadership of the practice, in the real project to extract the topic, here only one of the difficulty of the lower middle of the topic, put aside the interview not to talk about, a few rounds down the written test is not ideal, have not found anyone can write logic watertight and maximize the performance of the script, difficult </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 three framework integration process (for reference only) </title> &NBsp;<summary type= "Text" > three framework as the name implies is very famous,, the framework of many methods of integration, now I write a very simple integration process, I believe you will see! Here, the first step, build the environment, import </summary> <published>2014-12-04t15:13:00+ 08:00</published> <updated>2014-12-04T08:25:04Z</updated> <author> < name> Guecheng Blog Park </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>
Transformation
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 results are as follows
{"Feed": {"xmlns": "Http://www.w3.org/2005/Atom", "title": "Blog Park", "id": "uuid:2c932236-94ae-434b-a607-299000 40a1fd;id=8360 "," Updated ":" 2014-12-04t08:25:04z "," link ": {" href ":" http://www.cnblogs.com/"}," en Try ": [{" id ":" 4143069 "," title ":" How would you write this SQL pen question "," Summary ":" Recently interviewed a number of candidates, industry experience three years to seven years ranging, initially felt that this No need to prepare pen questions, due to the leadership of the practice, in the real project to extract the topic, here just to say one of the difficulty of the lower middle of the topic, put aside interview not to talk, a single look at the written test words a few rounds down the answer is not ideal, have not found anyone can write logic watertight and maximize the performance of the script 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": "One"}, {"id": "4133547", "t Itle ":" S2SH three framework integration process (for reference only) ", "Summary": "The three framework as the name implies is very famous,, the framework of many methods of integration, now I write a very simple integration process, I believe you will see!" Here use, first step, build environment, import "," published ":" 2014-12-04t15:13:00+08:00 "," Updated ":" 2014-12-04t08:25:04z ", "Author": {"name": "Guecheng Blog Park", "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 into JSON format