1 回答
TA贡献1886条经验 获得超2个赞
您可以实现自定义封送拆收器。
type InventoryItem struct {
XMLName xml.Name
ItemName string `xml:"Name"`
ItemDescription string `xml:"Description"`
}
func (i InventoryItem) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// Ignore the passed in `start` argument, it represents the
// parent InventoryItem element of the Name and Description.
// Declare types to represent the elements you want to encode,
// initialize them to the item's field values and encode them.
//
// Use the ",chardata" option to tell the encoder to encode the
// field's value directly rather than as a child element.
type Name struct {
Value string `xml:",chardata"`
}
if err := e.Encode(Name{i.ItemName}); err != nil {
return err
}
type Description struct {
Value string `xml:",chardata"`
}
return e.Encode(Description{i.ItemDescription})
}
https://play.golang.org/p/D4ZVr2sWZju
- 1 回答
- 0 关注
- 73 浏览
添加回答
举报