GuLiMmo
2024-03-19 aec00ecc093be803860c8675cbe1c4c776a7cb4e
src/utils/cesium/kmz.ts
@@ -112,14 +112,22 @@
          // 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)
            }
          }
        }
      }
@@ -145,7 +153,7 @@
    return xmlObj
  } else {
    return {
      Document: xmlObj
      Document: xmlObj,
    }
  }
}
@@ -160,22 +168,23 @@
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}>`
          }
        }
      })
@@ -186,11 +195,11 @@
  }
  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
}