简体   繁体   English

如何将JSON数据转换为XML格式数据并下载javascript中的文件

[英]How to convert JSON data to XML format data and download the file in javascript

CODE converting json to xml but not as required as i wanted what should i add to add those two lines and download the xml format file代码将 json 转换为 xml 但不是我想要的我应该添加什么来添加这两行并下载 xml 格式文件

function json2xml(json) {
var a = json;
var c = document.createElement("resources");
var t = function (v) {
    return {}.toString.call(v).split(' ')[1].slice(0, -1).toLowerCase();
};
var f = function (f, c, a, s) {
    c.setAttribute("tools", "http://schemas.android.com/tools");

if (t(a) != "array" && t(a) != "object") {
        if (t(a) != "null") {
            c.appendChild(document.createTextNode(a));
        }
    } else {
        for (var k in a) {
            var v = a[k];
            if (k == "ki" && t(a) == "object") {
                c.setAttribute("__pi", v);
            } else {
                if (t(v) == "object") {
                    var ch = c.appendChild(document.createElementNS(null, s ? "bh" : "string"));
                    f(f, ch, v);
                } else if (t(v) == "array") {
                    var ch = c.appendChild(document.createElementNS(null, s ? "ni" : "string"));
                    f(f, ch, v, true);
                } else {
                    var va = document.createElementNS(null, s ? "ki" : "string");
                    if (t(v) != "null") {
                        va.appendChild(document.createTextNode(v));
                    }
                    var ch = c.appendChild(va);
                    ch.setAttribute("name", k);
                }
            }
        }
    }
};
f(f, c, a, t(a) == "array");
console.log(c.outerHTML);
return c.outerHTML;

} }

OUTPUT-输出-

<resources tools="http://schemas.android.com/tools">
<string name="app_name">translations</string>
<string name="bread">jam</string>
<string name="flew">doctor</string>
<string name="version">Version</string>
</resources>

Required output-所需的输出 -

<?xml version="1.0" encoding="utf-8"?>
<resources tools="http://schemas.android.com/tools" tools:ignore="MissingTranslation">
<string name="app_name">translations</string>
<string name="bread">jam</string>
<string name="flew">doctor</string>
<string name="version">Version</string>
</resources>

How to add first two lines and download the file in xml format?如何添加前两行并下载 xml 格式的文件?

Simply add attribute on resources and insert <?xml> string at the start.只需在资源上添加属性并在开头插入<?xml>字符串。

function json2xml(json) {
  var a = json;
  var c = document.createElement("resources");
  var t = function (v) {
    return {}.toString.call(v).split(' ')[1].slice(0, -1).toLowerCase();
  };
  var f = function (f, c, a, s) {
    c.setAttribute("tools", "http://schemas.android.com/tools");
    c.setAttribute("tools:ignore", "MissingTranslation");

    if (t(a) != "array" && t(a) != "object") {
      if (t(a) != "null") {
        c.appendChild(document.createTextNode(a));
      }
    } else {
      for (var k in a) {
        var v = a[k];
        if (k == "ki" && t(a) == "object") {
          c.setAttribute("__pi", v);
        } else {
          if (t(v) == "object") {
            var ch = c.appendChild(document.createElementNS(null, s ? "bh" : "string"));
            f(f, ch, v);
          } else if (t(v) == "array") {
            var ch = c.appendChild(document.createElementNS(null, s ? "ni" : "string"));
            f(f, ch, v, true);
          } else {
            var va = document.createElementNS(null, s ? "ki" : "string");
            if (t(v) != "null") {
              va.appendChild(document.createTextNode(v));
            }
            var ch = c.appendChild(va);
            ch.setAttribute("name", k);
          }
        }
      }
    }
  };
  f(f, c, a, t(a) == "array");
  var xml = '<?xml version="1.0" encoding="utf-8"?>' + c.outerHTML;
  console.log(xml);
  return xml;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM