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 |   33 +++++++++++++++++++++------------
 1 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/utils/cesium/kmz.ts b/src/utils/cesium/kmz.ts
index 6d90117..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)
+            }
           }
         }
       }
@@ -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
 }

--
Gitblit v1.9.3