下载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();
  },

生成的xml字符串格式化

参考:https://blog.csdn.net/qq_40323256/article/details/127036957icon-default.png?t=M85Bhttps://blog.csdn.net/qq_40323256/article/details/127036957

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐