0

我正在尝试使用带有一些声明和标签的 MarkupBuilder 构建一个 xml 文件,但我没有找到构建以下行的解决方案

<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">

在 xml 文件中。

我很感激任何帮助

代码

def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
xml.mkp.xmlDeclaration(version: "1.0", encoding: "utf-8")
xml.Documents {
    Placemark() {
        name("mapData")
        styleUrl("#m_ylw-pushpin")
        LineString(){
         tessellate("1") 
         coordinates("8.463415562903656,47.97716901704985,0 8.462984259089152,47.97656710950728,0 8.461242137735676,47.97352418265724,0 8.485274199626216")
          }

    }
}

println writer.toString()

例子

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
    </StyleMap>
    <Placemark>
        <name>mapData</name>
        <styleUrl>#m_ylw-pushpin</styleUrl>
        <LineString>
            <tessellate>1</tessellate>
            <coordinates>
                8.463415562903656,47.97716901704985,0 8.462984259089152,47.97656710950728,0 8.461242137735676,47.97352418265724,0 8.485274199626216
           </coordinates>
        </LineString>
    </Placemark>
</Document>
</kml>

电流输出

<?xml version='1.0' encoding='utf-8'?>
<Documents>
  <Placemark>
    <name>mapData</name>
    <styleUrl>#m_ylw-pushpin</styleUrl>
    <LineString>
      <tessellate>1</tessellate>
      <coordinates>8.463415562903656,47.97716901704985,0 8.462984259089152,47.97656710950728,0 8.461242137735676,47.97352418265724,0 8.485274199626216</coordinates>
    </LineString>
  </Placemark>
</Documents>
4

1 回答 1

1

添加kml为顶级元素,添加Document为子元素并setDoubleQuotes(true)在构建器上调用以输出双引号而不是单引号

def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer)
xml.doubleQuotes = true 
xml.mkp.xmlDeclaration version: "1.0", encoding: "utf-8"
xml.kml ( xmlns:"http://www.opengis.net/kml/2.2",
    "xmlns:gx": "http://www.google.com/kml/ext/2.2",
    "xmlns:kml": "http://www.opengis.net/kml/2.2",
    "xmlns:atom": "http://www.w3.org/2005/Atom"
) {
    Documents {
        Placemark() {
            name "mapData"
            styleUrl "#m_ylw-pushpin"
            LineString {
                tessellate "1" 
                coordinates "8.463415562903656,47.97716901704985,0 8.462984259089152,47.97656710950728,0 8.461242137735676,47.97352418265724,0 8.485274199626216"
            }
        }
    }
}

println writer.toString()

输出:

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
  <Documents>
    <Placemark>
      <name>mapData</name>
      <styleUrl>#m_ylw-pushpin</styleUrl>
      <LineString>
        <tessellate>1</tessellate>
        <coordinates>8.463415562903656,47.97716901704985,0 8.462984259089152,47.97656710950728,0 8.461242137735676,47.97352418265724,0 8.485274199626216</coordinates>
      </LineString>
    </Placemark>
  </Documents>
</kml>
于 2016-08-21T09:27:34.853 回答