I need to store coordinates to KML file. It should be like this:
<coordinates>
51.600223,25.003058,0
51.600223,25.003065,0
51.600213,25.003065,0
51.600212,25.003065,0
51.600217,25.003075,0
</coordinates>
If I join the string and try to marshal as one string I get 
 for every new line
KmlLineString struct {
Coordinates string `xml:"coordinates"`
}
If I change it to Coordinates []string`xml:"coordinates"` I get tags on every line.
How can I do this?
CodePudding user response:
As @icza points out in his comment, the 
 is an escaped new line and is the correct output when marshaling XML. Any valid XML decoder will be able to unmarshal that and understand what it represents. Another salient point made by @icza is that if the field flagged with ,innerxml contains invalid XML, the result of marshaling such a field will also be invalid XML as it will not be escaped.
If you want to output the literal new lines regardless, perhaps to make the XML human-friendly, then you can use the ,innerxml tag option. This option instructs the encoder to marshal the content verbatim.
For example:
var xml_data = `<coordinates>
51.600223,25.003058,0
51.600223,25.003065,0
51.600213,25.003065,0
51.600212,25.003065,0
51.600217,25.003075,0
</coordinates>`
type KmlLineString struct {
Coordinates string `xml:",innerxml"`
}
https://go.dev/play/p/kXOjZZ-DyHc
Or
var xml_data = `
51.600223,25.003058,0
51.600223,25.003065,0
51.600213,25.003065,0
51.600212,25.003065,0
51.600217,25.003075,0
`
type KmlLineString struct {
Coordinates Coordinates `xml:"coordinates"`
}
type Coordinates struct {
Value string `xml:",innerxml"`
}
