下载x2js
cnpm i -S x2js
全局引入
import x2js from 'x2js'
Vue.prototype.$x2js = new x2js()
使用
mounted() {
console.log("this.$x2js:", this.$x2js);
let xmlstr = `<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>`;
console.log("xmlstr:", xmlstr);
console.log("xml转json:", this.$x2js.xml2js(xmlstr));
console.log("json转xml:", this.$x2js.js2xml(this.$x2js.xml2js(xmlstr)));
},
下载xml文件到本地
mounted() {
let xmlstr = `<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>`;
let url = window.URL.createObjectURL(
new Blob([xmlstr], { type: "text/xml" })
);
// 然后就可以创建a标签 最后下载下来了
let link = document.createElement("a");
// 不显示链接
link.style.display = "none";
link.href = url;
// 设置链接属性
link.setAttribute("download", "xmlstr");
//点击链接
document.body.appendChild(link);
link.click();
},
?
|