本地上传xml

dev
陈博文 3 years ago
parent 4d0ebe7503
commit 4953dcb997

@ -18,6 +18,8 @@ import { downLoadLink } from '@/utils/download';
import url from '@/utils/url';
import { ColumnsType } from 'antd/lib/table';
import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
import { uniqWith, isEqual } from 'lodash';
const { Dragger } = Upload;
interface PageProps {}
@ -242,6 +244,75 @@ const Page: FC<PageProps> = () => {
);
};
const loadXML = function (file: any) {
const item: any = [];
const cb = () => {
console.log(item, 22222);
const dealItem = uniqWith(item, isEqual);
console.log(dealItem, 3333);
};
// 将 xml 报文转化为dom对象注意去掉xml的协议头
const parseXML = (xml: any) => {
return new DOMParser().parseFromString(xml, 'text/xml');
};
// XML 转为 Json对象
const xml2Json = (xmlString: any) => {
let xmlDoc = parseXML(xmlString);
// 准备JSON字符串和缓存提升性能
//获取xml文档的所有子节点
var nodeList = xmlDoc.childNodes;
let buffer = {};
buffer = getXML(nodeList);
function getXML(nodes: any) {
let tmp: any = {};
for (let i in nodes) {
var curNode = nodes[i];
if (curNode.nodeType === 1) {
//如果子节点还包括子节点,则继续进行遍历
if (curNode.childNodes.length > 1) {
if (tmp.hasOwnProperty(curNode.nodeName)) {
let temp = getXML(curNode.childNodes);
if (Array.isArray(tmp[curNode.nodeName])) {
tmp[curNode.nodeName] = [...tmp[curNode.nodeName], temp];
} else {
tmp[curNode.nodeName] = [tmp[curNode.nodeName], temp];
}
} else {
tmp[curNode.nodeName] = getXML(curNode.childNodes);
}
} else {
var firstChild = curNode.childNodes[0];
if (firstChild != null) {
// nodeValue不为null
tmp[curNode.nodeName] = firstChild.nodeValue;
} else {
// nodeValue为null
tmp[curNode.nodeName] = '';
}
}
}
}
return tmp;
}
return buffer;
};
const xmlText = (xmlFile: any, cb: any) => {
const reader = new FileReader();
reader.readAsText(xmlFile.file, 'utf8');
reader.onload = () => {
item.push(xml2Json(reader.result));
if (item.length === file.length) {
cb();
}
};
};
for (let i = 0; i < file.length; i++) {
xmlText(file[i], cb);
}
};
const handleLoad = () => {
const k = [
{
@ -256,6 +327,7 @@ const Page: FC<PageProps> = () => {
formAdd.setFieldsValue({
sights: k,
});
loadXML(fileList);
};
return (

Loading…
Cancel
Save