我有一個關于編組 Go XML 的問題:我明白了:<root abc=""> <element></element></root>但我想要這樣:<root> <element abc=""></element></root>(該屬性abc位于子元素處)。這(容易)可能嗎?我的代碼:package mainimport ( "encoding/xml" "fmt" "os")type foo struct { XMLName xml.Name `xml:"root"` Abc string `xml:"abc,attr"` Element string `xml:"element"`}func main() { f := foo{} a, err := xml.MarshalIndent(f, "", " ") if err != nil { fmt.Println(err) os.Exit(0) } fmt.Println(string(a))}
1 回答

天涯盡頭無女友
TA貢獻1831條經驗 獲得超9個贊
您可以像這樣定義結構:
type foo struct {
XMLName xml.Name `xml:"root"`
Element struct{
xml.Name `xml:"element"`
Abc string `xml:"abc,attr"`
}
}
- 1 回答
- 0 關注
- 142 瀏覽
添加回答
舉報
0/150
提交
取消