vue如何解析xml文件 x2js


好久沒來寫東西了,主要是一直在加班,哼哼,不開心

項目中會用到將xml文件解析成json文件在頁面中顯示出來,以前jq的時候用到的方法行不通了,故在這邊介紹一種我覺得還不錯的插件

1. npm安裝

 npm i x2js

2.在main.js中引用

import x2js from 'x2js' //xml數據處理插件
Vue.prototype.$x2js = new x2js() //創建x2js對象,掛到vue原型上

3.在組件中使用

 _getVersion(url) {
      this.loading = true;
      let _self = this;
      this.$ajax.get(`/data/version${url}.xml`).then(function(res) {
        _self.loading = false;
          console.log(res.data);
          /* var x2js = new X2JS();*/
          /* var obj = x2js.xml_str2json(res.data).note;*/
          var jsonObj = _self.$x2js.xml2js(res.data);
          console.log("-----");
          console.log(jsonObj.note);
          _self.tableData = jsonObj.note.specialityList.item;
          _self.changeData = jsonObj.note.changeList.item;
          _self.finishData = jsonObj.note.finishedPunchList.item;
          _self.knownData = jsonObj.note.questionList.item;
          _self.versionDes =  jsonObj.note.name;
          _self.versionDate = jsonObj.note.date;

        })
        .catch(function(err) {
          console.log(err)
          _self.loading = false;
        });
    }

注意:你的xml文件如果是放在前端這邊的話,要把文件放到public文件夾當中,否則文件將會報404的錯

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM