TheBook
TheBook

Reputation: 1708

add some declaration line to the xml file with the aid of MarkupBuilder

I am trying to build a xml file with the MarkupBuilder with some declarations and tags but I do not find a solution to build the following line

<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">

in the xml file.

I appreciate any help

code

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()

example

<?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>

Current output

<?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>

Upvotes: 0

Views: 937

Answers (1)

Gergely Toth
Gergely Toth

Reputation: 6977

Add kml as top level element, add Document as child to it and invoke setDoubleQuotes(true) on the builder to output double quotes instead of single

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()

Output:

<?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>

Upvotes: 1

Related Questions