|
|
@@ -125,6 +125,36 @@ const ImportXML = (() => {
|
|
|
info: 'priceInfo', //造价信息差额调整法
|
|
|
coe: 'priceCoe' //价格指数调整法
|
|
|
};
|
|
|
+ // xml转义字符
|
|
|
+ const XML_CHARS = {
|
|
|
+ ' ': 'escape{space}',
|
|
|
+ ' ': 'escape{simpleSpace}',
|
|
|
+ '	': 'escape{tab}',
|
|
|
+ '	': 'escape{simpleTab}',
|
|
|
+ '
': 'escape{return}',
|
|
|
+ '
': 'escape{simpleReturn}',
|
|
|
+ '�A;': 'escape{newLine}',
|
|
|
+ '
': 'escape{simpleNewLine}',
|
|
|
+ '<': 'escape{less}',
|
|
|
+ '>': 'escape{greater}',
|
|
|
+ '&': 'escape{and}',
|
|
|
+ '"': 'escape{quot}',
|
|
|
+ ''': 'escape{apos}'
|
|
|
+ };
|
|
|
+ // 避免转义字符进行转义。原文本中含有xml转义字符,转换为其他字符。
|
|
|
+ function escapeXMLChars(str) {
|
|
|
+ for (let [key, value] of Object.entries(XML_CHARS)) {
|
|
|
+ str = str.replace(new RegExp(key, 'g'), value);
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
+ // 还原避免字符为转义字符
|
|
|
+ function restoreXMLChars(str) {
|
|
|
+ for (let [key, value] of Object.entries(XML_CHARS)) {
|
|
|
+ str = str.replace(new RegExp(value, 'g'), key);
|
|
|
+ }
|
|
|
+ return str;
|
|
|
+ }
|
|
|
|
|
|
//读取文件转换为utf-8编码的字符串
|
|
|
function readAsTextSync(file) {
|
|
|
@@ -1307,12 +1337,16 @@ const ImportXML = (() => {
|
|
|
}
|
|
|
}
|
|
|
//从xml文件中提取数据
|
|
|
- this.extractData = async (file) => {
|
|
|
+ this.extractData = async (file, escape) => {
|
|
|
//将二进制文件转换成字符串
|
|
|
let xmlStr = await readAsTextSync(file);
|
|
|
//将xml格式良好的字符串转换成对象
|
|
|
let x2js = new X2JS();
|
|
|
+ if (escape) {
|
|
|
+ xmlStr = escapeXMLChars(xmlStr);
|
|
|
+ }
|
|
|
let xmlObj = x2js.xml_str2json(xmlStr);
|
|
|
+ xmlObj = JSON.parse(restoreXMLChars(JSON.stringify(xmlObj)));
|
|
|
if (!xmlObj) {
|
|
|
throw '无有效数据。';
|
|
|
}
|