From aec00ecc093be803860c8675cbe1c4c776a7cb4e Mon Sep 17 00:00:00 2001
From: GuLiMmo <2820890765@qq.com>
Date: Tue, 19 Mar 2024 17:49:21 +0800
Subject: [PATCH] update: 新建航线、事件编辑、kmz文件问题修改

---
 src/utils/cesium/kmz.ts |   41 ++++++++++++++++++++++++++++-------------
 1 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/src/utils/cesium/kmz.ts b/src/utils/cesium/kmz.ts
index cf481bc..84d9435 100644
--- a/src/utils/cesium/kmz.ts
+++ b/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)
+            }
           }
         }
       }
@@ -141,7 +149,13 @@
   const parser = new DOMParser()
   const xmlDoc = parser.parseFromString(xmlStr, 'text/xml')
   const xmlObj = deepParse(xmlDoc.documentElement)
-  return xmlObj
+  if (Reflect.has(xmlObj, 'Document')) {
+    return xmlObj
+  } else {
+    return {
+      Document: xmlObj,
+    }
+  }
 }
 
 // json转xml
@@ -151,24 +165,25 @@
  * @param {string} rootName xml根节点名称
  * @return {*} string
  */
-export const JSONToXML = (obj: any, rootName?: string | undefined) => {
+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}>`
+        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) ? 'wpml:' : ''
             xml += `<${str}${key}>${buildXml(obj[key], key)}</${str}${key}>`
           }
         }
@@ -179,12 +194,12 @@
     return xml
   }
 
-  xml += `
-  <?xml version="1.0" encoding="UTF-8"?>
+  const header = isEnd ? '<?xml version="1.0" encoding="UTF-8"?>' : ''
+  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
 }

--
Gitblit v1.9.3