我需要將坐標存盤到 KML 檔案中。應該是這樣的:
<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>
如果我加入字串并嘗試將
每個新行編組為一個字串
KmlLineString struct {
Coordinates string `xml:"coordinates"`
}
如果我將其更改為Coordinates []string`xml:"coordinates"` 每行都會得到標簽。
我怎樣才能做到這一點?
uj5u.com熱心網友回復:
正如@icza 在他的評論中指出的那樣,這
是一個轉義的新行,并且是編組 XML 時的正確輸出。任何有效的 XML 解碼器都能夠解組它并理解它代表什么。
如果您想無論如何都輸出文字新行,也許是為了使 XML 人性化,那么您可以使用,innerxmltag 選項。此選項指示編碼器逐字編組內容。但是,正如@icza 在另一條評論中指出的那樣,如果標記為 的欄位,innerxml包含無效的 XML,則封送此類欄位的結果也將是無效的 XML。
例如:
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
或者
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"`
}
https://go.dev/play/p/kiN-NUt67ny
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/404479.html
標籤:
上一篇:Echo跳過JWT中間件
下一篇:如何從變數宣告常量?
