【问题标题】:add some declaration line to the xml file with the aid of MarkupBuilder借助 MarkupBuilder 在 xml 文件中添加一些声明行
【发布时间】:2016-08-20 17:47:48
【问题描述】:

我正在尝试使用带有一些声明和标签的 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>

【问题讨论】:

  • 如果您以 xml.kml { Documents { ... } } 开始文档并按照重复帖子中的示例进行操作,答案应该是直截了当的。
  • @MichaelEaster:但我怎样才能得到标签中的双引号?我总是得到单引号 xmlns='http://www.opengis.net/kml/2.2' 它必须看起来像 xmlns="http://www.opengis.net/kml/2.2"

标签: xml groovy markupbuilder


【解决方案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>

【讨论】:

    猜你喜欢
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多