Skip to content

Commit 8e3e54c

Browse files
实现xml转json
1 parent a1c0344 commit 8e3e54c

5 files changed

Lines changed: 60 additions & 10 deletions

File tree

package-lock.json

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"core-js": "^3.6.5",
1212
"element-ui": "^2.13.2",
1313
"exports-loader": "^1.1.0",
14+
"jquery": "^3.5.1",
1415
"less-loader": "^6.1.2",
1516
"lodash": "^4.17.20",
1617
"mxgraph": "^4.2.0",
@@ -19,7 +20,8 @@
1920
"vue": "^2.6.11",
2021
"vue-codemirror": "^4.0.6",
2122
"vue-json-viewer": "^2.2.14",
22-
"vue-router": "^3.3.4"
23+
"vue-router": "^3.3.4",
24+
"x2js": "^3.4.0"
2325
},
2426
"devDependencies": {
2527
"@vue/cli-plugin-babel": "~4.4.0",

src/main.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import JsonViewer from 'vue-json-viewer'
99
Vue.use(JsonViewer)
1010
Vue.use(ElementUI);
1111
Vue.config.productionTip = false
12-
1312
new Vue({
1413
render: h => h(App),
1514
router

src/views/customToolbar/component/uploadData.vue

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<el-dialog
33
:title="isOutputXml ? '数据导出':'数据导入'"
44
:visible.sync="centerDialogVisible"
5-
width="70%"
5+
width="70%;"
66
center
77
@close="close"
88
>
@@ -30,12 +30,24 @@
3030
</div>
3131
</el-upload>
3232
<!--codemirror插件-->
33-
<codemirror
34-
:value="currentNodeData"
35-
v-model="currentNodeData"
36-
:options="options"
37-
class="codemirror"
38-
></codemirror>
33+
<div style="display:flex">
34+
<codemirror
35+
:value="currentNodeData"
36+
v-model="currentNodeData"
37+
:options="options"
38+
:style="{'width': isOutputXml ? '50%':'100%'}"
39+
class="codemirror"
40+
></codemirror>
41+
<!-- xml转json -->
42+
<json-viewer
43+
v-if="isOutputXml"
44+
class="xmlToJson"
45+
:value="xml2json"
46+
:expand-depth="5"
47+
copyable
48+
sort
49+
></json-viewer>
50+
</div>
3951
<span slot="footer" class="dialog-footer">
4052
<el-button @click="cancelUpload">取 消</el-button>
4153
<el-button type="primary" @click="confirmUpload" v-if="!isOutputXml">导入</el-button>
@@ -69,11 +81,13 @@ import 'codemirror/addon/fold/foldcode'
6981
import 'codemirror/addon/fold/foldgutter'
7082
import 'codemirror/addon/fold/brace-fold'
7183
import 'codemirror/addon/fold/comment-fold'
84+
import X2JS from '../x2js'
7285
7386
export default {
7487
props: ['graphXml', 'isOutputXml'],
7588
data () {
7689
return {
90+
xml2json: '',
7791
uploadFiles: [],
7892
centerDialogVisible: false,
7993
currentNodeData: "",
@@ -99,7 +113,11 @@ export default {
99113
watch: {
100114
currentNodeData: {
101115
handler (newvalue) {
102-
this.currentNodeData = newvalue
116+
// let xmljson = x2js.xml2js(newvalue) //xml2js方法,传入xml格式的数据,返回json对象
117+
//创建一个x2js对象进行转换
118+
var x2js = new X2JS();
119+
this.xml2json = x2js.xml_str2json(newvalue);
120+
// this.currentNodeData = newvalue
103121
},
104122
immediate: true,
105123
deep: true
@@ -110,6 +128,7 @@ export default {
110128
this.currentNodeData = this.graphXml
111129
},
112130
methods: {
131+
113132
close () {
114133
this.centerDialogVisible = false
115134
this.$parent.uploadDataVisible = false;
@@ -189,4 +208,15 @@ export default {
189208
left: 10px;
190209
content: "数据预览 :";
191210
}
211+
.xmlToJson {
212+
width: 50%;
213+
height: 100%;
214+
}
215+
.xmlToJson::before {
216+
position: absolute;
217+
font-size: 17px;
218+
top: 0px;
219+
left: 10px;
220+
content: "XML转JSON :";
221+
}
192222
</style>

src/views/customToolbar/x2js.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)