1 回答

TA貢獻2012條經驗 獲得超12個贊
用XMLName在你的OrderRaw:
type OrderRaw struct {
XMLName xml.Name `xml:"order"`
Order string `xml:",innerxml"`
OrderID string `xml:"order-id,attr"`
}
游樂場: https: //play.golang.org/p/onK5FExqzD。
編輯:如果要保存所有屬性,則必須使用MarshalerXML和UnmarshalerXML接口:
func (o *OrderRaw) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
o.attrs = map[xml.Name]string{}
for _, a := range start.Attr {
o.attrs[a.Name] = a.Value
}
type order OrderRaw
return d.DecodeElement((*order)(o), &start)
}
func (o OrderRaw) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
for name, attr := range o.attrs {
start.Attr = append(start.Attr, xml.Attr{Name: name, Value: attr})
}
start.Name = xml.Name{Local: "order"}
type order OrderRaw
return e.EncodeElement(order(o), start)
}
游樂場: https: //play.golang.org/p/XhkwqJFyMd。
- 1 回答
- 0 關注
- 183 瀏覽
添加回答
舉報