2025-07-25 13:36:17 +08:00
|
|
|
<template>
|
2025-07-28 17:01:13 +08:00
|
|
|
<div>
|
|
|
|
|
<div ref="editor" style="text-align:left"></div>
|
|
|
|
|
<button @click="exportToXml">导出为XML</button>
|
|
|
|
|
<input type="file" accept=".xml" @change="handleXmlUpload">
|
2025-07-25 13:36:17 +08:00
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
2025-07-28 17:01:13 +08:00
|
|
|
import E from 'wangeditor';
|
|
|
|
|
import { js2xml, xml2js } from 'xml-js';
|
2025-07-25 13:36:17 +08:00
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
2025-07-28 17:01:13 +08:00
|
|
|
editor: null
|
|
|
|
|
};
|
2025-07-25 13:36:17 +08:00
|
|
|
},
|
|
|
|
|
mounted() {
|
2025-07-28 17:01:13 +08:00
|
|
|
this.editor = new E(this.$refs.editor);
|
|
|
|
|
this.editor.create();
|
2025-07-25 13:36:17 +08:00
|
|
|
},
|
|
|
|
|
methods: {
|
2025-07-28 17:01:13 +08:00
|
|
|
convertHtmlToXml(htmlContent) {
|
|
|
|
|
const xmlObject = {
|
|
|
|
|
declaration: {
|
|
|
|
|
attributes: {
|
|
|
|
|
version: '1.0',
|
|
|
|
|
encoding: 'UTF-8'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
elements: [{
|
|
|
|
|
type: 'element',
|
|
|
|
|
name: 'richText',
|
|
|
|
|
elements: [{
|
|
|
|
|
type: 'text',
|
|
|
|
|
text: htmlContent
|
|
|
|
|
}]
|
|
|
|
|
}]
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return js2xml(xmlObject, { compact: false, spaces: 4 });
|
2025-07-25 13:36:17 +08:00
|
|
|
},
|
|
|
|
|
|
2025-07-28 17:01:13 +08:00
|
|
|
downloadXml(content, fileName = 'content.xml') {
|
|
|
|
|
const blob = new Blob([content], { type: 'application/xml' });
|
|
|
|
|
const link = document.createElement('a');
|
|
|
|
|
link.href = URL.createObjectURL(blob);
|
|
|
|
|
link.download = fileName;
|
|
|
|
|
link.click();
|
|
|
|
|
URL.revokeObjectURL(link.href);
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
exportToXml() {
|
|
|
|
|
const html = this.editor.txt.html();
|
|
|
|
|
const xml = this.convertHtmlToXml(html);
|
|
|
|
|
this.downloadXml(xml);
|
2025-07-25 13:36:17 +08:00
|
|
|
},
|
|
|
|
|
|
2025-07-28 17:01:13 +08:00
|
|
|
parseXmlToHtml(xmlString) {
|
2025-07-25 13:36:17 +08:00
|
|
|
try {
|
2025-07-28 17:01:13 +08:00
|
|
|
const result = xml2js(xmlString, { compact: false });
|
|
|
|
|
const richTextElement = result.elements.find(el => el.name === 'richText');
|
|
|
|
|
if (richTextElement && richTextElement.elements && richTextElement.elements.length > 0) {
|
|
|
|
|
return richTextElement.elements[0].text;
|
2025-07-25 13:36:17 +08:00
|
|
|
}
|
2025-07-28 17:01:13 +08:00
|
|
|
return '';
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('XML解析错误:', e);
|
|
|
|
|
return '';
|
2025-07-25 13:36:17 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
2025-07-28 17:01:13 +08:00
|
|
|
handleXmlUpload(event) {
|
|
|
|
|
const file = event.target.files[0];
|
|
|
|
|
if (!file) return;
|
2025-07-25 13:36:17 +08:00
|
|
|
|
2025-07-28 17:01:13 +08:00
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onload = (e) => {
|
|
|
|
|
const xmlString = e.target.result;
|
|
|
|
|
const html = this.parseXmlToHtml(xmlString);
|
|
|
|
|
if (html) {
|
|
|
|
|
this.editor.txt.html(html);
|
2025-07-25 13:36:17 +08:00
|
|
|
}
|
2025-07-28 17:01:13 +08:00
|
|
|
};
|
|
|
|
|
reader.readAsText(file);
|
2025-07-25 13:36:17 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
beforeDestroy() {
|
|
|
|
|
if (this.editor) {
|
2025-07-28 17:01:13 +08:00
|
|
|
this.editor.destroy();
|
2025-07-25 13:36:17 +08:00
|
|
|
}
|
|
|
|
|
}
|
2025-07-28 17:01:13 +08:00
|
|
|
};
|
|
|
|
|
</script>
|