| | |
| | | // element node |
| | | // 递归调用处理子节点 |
| | | if (obj[nodeName] === undefined) { |
| | | obj[nodeName] = deepParse(item) |
| | | const nodeValue = deepParse(item) |
| | | if (nodeValue[nodeName] === undefined) { |
| | | obj[nodeName] = nodeValue |
| | | } else { |
| | | obj[nodeName] = nodeValue[nodeName] |
| | | } |
| | | } else { |
| | | if (!obj[nodeName].push) { |
| | | const old = obj[nodeName] |
| | | obj[nodeName] = [] |
| | | obj[nodeName].push(old) |
| | | } |
| | | obj[nodeName].push(deepParse(item)) |
| | | const nodeValue = deepParse(item) |
| | | if (nodeValue[nodeName] === undefined) { |
| | | obj[nodeName].push(nodeValue) |
| | | } |
| | | } |
| | | } |
| | | } |
| | |
| | | return xmlObj |
| | | } else { |
| | | return { |
| | | Document: xmlObj |
| | | Document: xmlObj, |
| | | } |
| | | } |
| | | } |
| | |
| | | export const JSONToXML = (obj: any, rootName?: string | undefined, isEnd: boolean = false) => { |
| | | let xml = '' |
| | | |
| | | const buildXml = (obj: string | any[], rootName?: string | undefined) => { |
| | | const buildXml = (obj: string | any[], rootName: string = '') => { |
| | | let xml = '' |
| | | if (Array.isArray(obj)) { |
| | | obj.forEach((item) => { |
| | | xml += `<${rootName}>${buildXml(item)}</${rootName}>\n` |
| | | const str = !noWmpl.includes(rootName) && isEnd ? 'wpml:' : '' |
| | | xml += `<${str}${rootName}>${buildXml(item)}</${str}${rootName}>` |
| | | }) |
| | | } else if (typeof obj === 'object') { |
| | | Object.keys(obj).forEach((key) => { |
| | | if (key === '#text') { |
| | | xml += obj[key] |
| | | } else { |
| | | const str = !noWmpl.includes(key) && isEnd ? 'wpml:' : '' |
| | | if (key === 'Placemark') { |
| | | xml += `${buildXml(obj[key], key)}` |
| | | } else { |
| | | const str = !noWmpl.includes(key) && isEnd ? 'wpml:' : '' |
| | | xml += `<${str}${key}>${buildXml(obj[key], key)}</${str}${key}>\n` |
| | | xml += `<${str}${key}>${buildXml(obj[key], key)}</${str}${key}>` |
| | | } |
| | | } |
| | | }) |
| | |
| | | } |
| | | |
| | | const header = isEnd ? '<?xml version="1.0" encoding="UTF-8"?>' : '' |
| | | xml += ` |
| | | ${header} |
| | | xml += `${header} |
| | | <kml xmlns="http://www.opengis.net/kml/2.2" xmlns:wpml="http://www.dji.com/wpmz/1.0.5"> |
| | | ${buildXml(obj, rootName)} |
| | | </kml> |
| | | ` |
| | | <Document> |
| | | ${buildXml(obj, rootName)} |
| | | </Document> |
| | | </kml>` |
| | | return xml |
| | | } |