借助 MarkupBuilder 向 xml 文件添加一些声明行
add some declaration line to the xml file with the aid of MarkupBuilder
我正在尝试使用带有一些声明和标记的 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>
添加 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>
我正在尝试使用带有一些声明和标记的 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>
添加 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>