使用js对json数据和xml数据的转化

function xml2json(xml,  // element or document DOM node
                  tab)  // tab or indent string for pretty output formatting
                        // omit or use empty string "" to supress.
                        // returns JSON string

function json2xml(obj,  // javascript object 
                  tab)  // tab or indent string for pretty output formatting
                        // omit or use empty string "" to supress.
                        // returns XML string
[javascript] view plaincopy
  1. <pre name="code" class="javascript">/*  This work is licensed under Creative Commons GNU LGPL License. 
  2.  
  3.     License: http://creativecommons.org/licenses/LGPL/2.1/ 
  4.    Version: 0.9 
  5.     Author:  Stefan Goessner/2006 
  6.     Web:     http://goessner.net/  
  7. */  
  8. function json2xml(o, tab) {  
  9.    var toXml = function(v, name, ind) {  
  10.       var xml = "";  
  11.       if (v instanceof Array) {  
  12.          for (var i=0, n=v.length; i<n; i++)  
  13.             xml += ind + toXml(v[i], name, ind+"\t") + "\n";  
  14.       }  
  15.       else if (typeof(v) == "object") {  
  16.          var hasChild = false;  
  17.          xml += ind + "<" + name;  
  18.          for (var m in v) {  
  19.             if (m.charAt(0) == "@")  
  20.                xml += " " + m.substr(1) + "=\"" + v[m].toString() + "\"";  
  21.             else  
  22.                hasChild = true;  
  23.          }  
  24.          xml += hasChild ? ">" : "/>";  
  25.          if (hasChild) {  
  26.             for (var m in v) {  
  27.                if (m == "#text")  
  28.                   xml += v[m];  
  29.                else if (m == "#cdata")  
  30.                   xml += "<![CDATA[" + v[m] + "]]>";  
  31.                else if (m.charAt(0) != "@")  
  32.                   xml += toXml(v[m], m, ind+"\t");  
  33.             }  
  34.             xml += (xml.charAt(xml.length-1)=="\n"?ind:"") + "</" + name + ">";  
  35.          }  
  36.       }  
  37.       else {  
  38.          xml += ind + "<" + name + ">" + v.toString() +  "</" + name + ">";  
  39.       }  
  40.       return xml;  
  41.    }, xml="";  
  42.    for (var m in o)  
  43.       xml += toXml(o[m], m, "");  
  44.    return tab ? xml.replace(/\t/g, tab) : xml.replace(/\t|\n/g, "");  
  45. }</pre>  
[javascript] view plaincopy
  1. /*  This work is licensed under Creative Commons GNU LGPL License. 
  2.  
  3.     License: http://creativecommons.org/licenses/LGPL/2.1/ 
  4.    Version: 0.9 
  5.     Author:  Stefan Goessner/2006 
  6.     Web:     http://goessner.net/  
  7. */  
  8. function xml2json(xml, tab) {  
  9.    var X = {  
  10.       toObj: function(xml) {  
  11.          var o = {};  
  12.          if (xml.nodeType==1) {   // element node ..  
  13.             if (xml.attributes.length)   // element with attributes  ..  
  14.                for (var i=0; i<xml.attributes.length; i++)  
  15.                   o["@"+xml.attributes[i].nodeName] = (xml.attributes[i].nodeValue||"").toString();  
  16.             if (xml.firstChild) { // element has child nodes ..  
  17.                var textChild=0, cdataChild=0, hasElementChild=false;  
  18.                for (var n=xml.firstChild; n; n=n.nextSibling) {  
  19.                   if (n.nodeType==1) hasElementChild = true;  
  20.                   else if (n.nodeType==3 && n.nodeValue.match(/[^ \f\n\r\t\v]/)) textChild++; // non-whitespace text  
  21.                   else if (n.nodeType==4) cdataChild++; // cdata section node  
  22.                }  
  23.                if (hasElementChild) {  
  24.                   if (textChild < 2 && cdataChild < 2) { // structured element with evtl. a single text or/and cdata node ..  
  25.                      X.removeWhite(xml);  
  26.                      for (var n=xml.firstChild; n; n=n.nextSibling) {  
  27.                         if (n.nodeType == 3)  // text node  
  28.                            o["#text"] = X.escape(n.nodeValue);  
  29.                         else if (n.nodeType == 4)  // cdata node  
  30.                            o["#cdata"] = X.escape(n.nodeValue);  
  31.                         else if (o[n.nodeName]) {  // multiple occurence of element ..  
  32.                            if (o[n.nodeName] instanceof Array)  
  33.                               o[n.nodeName][o[n.nodeName].length] = X.toObj(n);  
  34.                            else  
  35.                               o[n.nodeName] = [o[n.nodeName], X.toObj(n)];  
  36.                         }  
  37.                         else  // first occurence of element..  
  38.                            o[n.nodeName] = X.toObj(n);  
  39.                      }  
  40.                   }  
  41.                   else { // mixed content  
  42.                      if (!xml.attributes.length)  
  43.                         o = X.escape(X.innerXml(xml));  
  44.                      else  
  45.                         o["#text"] = X.escape(X.innerXml(xml));  
  46.                   }  
  47.                }  
  48.                else if (textChild) { // pure text  
  49.                   if (!xml.attributes.length)  
  50.                      o = X.escape(X.innerXml(xml));  
  51.                   else  
  52.                      o["#text"] = X.escape(X.innerXml(xml));  
  53.                }  
  54.                else if (cdataChild) { // cdata  
  55.                   if (cdataChild > 1)  
  56.                      o = X.escape(X.innerXml(xml));  
  57.                   else  
  58.                      for (var n=xml.firstChild; n; n=n.nextSibling)  
  59.                         o["#cdata"] = X.escape(n.nodeValue);  
  60.                }  
  61.             }  
  62.             if (!xml.attributes.length && !xml.firstChild) o = null;  
  63.          }  
  64.          else if (xml.nodeType==9) { // document.node  
  65.             o = X.toObj(xml.documentElement);  
  66.          }  
  67.          else  
  68.             alert("unhandled node type: " + xml.nodeType);  
  69.          return o;  
  70.       },  
  71.       toJson: function(o, name, ind) {  
  72.          var json = name ? ("\""+name+"\"") : "";  
  73.          if (o instanceof Array) {  
  74.             for (var i=0,n=o.length; i<n; i++)  
  75.                o[i] = X.toJson(o[i], "", ind+"\t");  
  76.             json += (name?":[":"[") + (o.length > 1 ? ("\n"+ind+"\t"+o.join(",\n"+ind+"\t")+"\n"+ind) : o.join("")) + "]";  
  77.          }  
  78.          else if (o == null)  
  79.             json += (name&&":") + "null";  
  80.          else if (typeof(o) == "object") {  
  81.             var arr = [];  
  82.             for (var m in o)  
  83.                arr[arr.length] = X.toJson(o[m], m, ind+"\t");  
  84.             json += (name?":{":"{") + (arr.length > 1 ? ("\n"+ind+"\t"+arr.join(",\n"+ind+"\t")+"\n"+ind) : arr.join("")) + "}";  
  85.          }  
  86.          else if (typeof(o) == "string")  
  87.             json += (name&&":") + "\"" + o.toString() + "\"";  
  88.          else  
  89.             json += (name&&":") + o.toString();  
  90.          return json;  
  91.       },  
  92.       innerXml: function(node) {  
  93.          var s = ""  
  94.          if ("innerHTML" in node)  
  95.             s = node.innerHTML;  
  96.          else {  
  97.             var asXml = function(n) {  
  98.                var s = "";  
  99.                if (n.nodeType == 1) {  
  100.                   s += "<" + n.nodeName;  
  101.                   for (var i=0; i<n.attributes.length;i++)  
  102.                      s += " " + n.attributes[i].nodeName + "=\"" + (n.attributes[i].nodeValue||"").toString() + "\"";  
  103.                   if (n.firstChild) {  
  104.                      s += ">";  
  105.                      for (var c=n.firstChild; c; c=c.nextSibling)  
  106.                         s += asXml(c);  
  107.                      s += "</"+n.nodeName+">";  
  108.                   }  
  109.                   else  
  110.                      s += "/>";  
  111.                }  
  112.                else if (n.nodeType == 3)  
  113.                   s += n.nodeValue;  
  114.                else if (n.nodeType == 4)  
  115.                   s += "<![CDATA[" + n.nodeValue + "]]>";  
  116.                return s;  
  117.             };  
  118.             for (var c=node.firstChild; c; c=c.nextSibling)  
  119.                s += asXml(c);  
  120.          }  
  121.          return s;  
  122.       },  
  123.       escape: function(txt) {  
  124.          return txt.replace(/[\\]/g, "\\\\")  
  125.                    .replace(/[\"]/g, '\\"')  
  126.                    .replace(/[\n]/g, '\\n')  
  127.                    .replace(/[\r]/g, '\\r');  
  128.       },  
  129.       removeWhite: function(e) {  
  130.          e.normalize();  
  131.          for (var n = e.firstChild; n; ) {  
  132.             if (n.nodeType == 3) {  // text node  
  133.                if (!n.nodeValue.match(/[^ \f\n\r\t\v]/)) { // pure whitespace text node  
  134.                   var nxt = n.nextSibling;  
  135.                   e.removeChild(n);  
  136.                   n = nxt;  
  137.                }  
  138.                else  
  139.                   n = n.nextSibling;  
  140.             }  
  141.             else if (n.nodeType == 1) {  // element node  
  142.                X.removeWhite(n);  
  143.                n = n.nextSibling;  
  144.             }  
  145.             else                      // any other node  
  146.                n = n.nextSibling;  
  147.          }  
  148.          return e;  
  149.       }  
  150.    };  
  151.    if (xml.nodeType == 9) // document node  
  152.       xml = xml.documentElement;  
  153.    var json = X.toJson(X.toObj(X.removeWhite(xml)), xml.nodeName, "\t");  
  154.    return "{\n" + tab + (tab ? json.replace(/\t/g, tab) : json.replace(/\t|\n/g, "")) + "\n}";  
  155. }  
以上内容均来自于:http://goessner.net/download/prj/jsonxml/

可以将json完美转化成xml,很好用,很强大 。

 

 

 

[代码][JavaScript]代码    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Changes XML to JSON
function xmlToJson(xml) {
     
    // Create the return object
    var obj = {};
 
    if (xml.nodeType == 1) { // element
        // do attributes
        if (xml.attributes.length > 0) {
        obj["@attributes"] = {};
            for (var j = 0; j < xml.attributes.length; j++) {
                var attribute = xml.attributes.item(j);
                obj["@attributes"][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") {
                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;
};

2. [代码][XML]代码    

1
2
3
4
5
6
7
8
9
10
11
12
<ALEXA VER="0.9" URL="davidwalsh.name/" HOME="0" AID="=">
    <SD TITLE="A" FLAGS="" HOST="davidwalsh.name">
        <TITLE TEXT="David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else"/>
        <LINKSIN NUM="1102"/>
        <SPEED TEXT="1421" PCT="51"/>
    </SD>
    <SD>
        <POPULARITY URL="davidwalsh.name/" TEXT="7131"/>
        <REACH RANK="5952"/>
        <RANK DELTA="-1648"/>
    </SD>
</ALEXA>

3. [代码]JSON结果     跳至 [1] [2] [3] [全屏预览]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{
  "@attributes": {
    AID: "=",
    HOME:  0,
    URL: "davidwalsh.name/",
    VER: "0.9",
  },
  SD = [
    {
      "@attributes": {
        FLAGS: "",
        HOST: "davidwalsh.name",
        TITLE: A
      },
      LINKSIN: {
        "@attributes": {
          NUM: 1102
        }
      },
      SPEED: {
        "@attributes": {
          PCT: 51,
          TEXT: 1421
        }
      },
      TITLE: {
        "@attributes": {
          TEXT: "David Walsh Blog :: PHP, MySQL, CSS, Javascript, MooTools, and Everything Else",
        }
      },
    },
    {
      POPULARITY: {
        "@attributes": {
          TEXT: 7131,
          URL: "davidwalsh.name/"
        }
      },
      RANK: {
        "@attributes": {
          DELTA: "-1648"
        }
      },
      REACH: {
        "@attributes": {
          RANK = 5952
        }
      }
    }
  ]
}
posted @ 2014-09-10 12:36  alxe_yu  阅读(437)  评论(0)    收藏  举报