1 回答
TA贡献1810条经验 获得超5个赞
把>.后mainCarriage场的标签,以表明你希望把字段的内容里面mainCarriage的标签。将Sea字段名称更改XMLName为 marshaller 的要求。
package main
import (
"encoding/xml"
"fmt"
"log"
)
type Carriage struct {
MainCarriage interface{} `xml:"mainCarriage>."`
}
type SeaCarriage struct {
XMLName xml.Name `xml:"http://www.example.com/XMLSchema/standard/2012 sea"`
LoadFactor float64 `xml:"loadFactor,attr"`
SeaCargoType string `xml:"seaCargoType,attr"`
}
type RoadCarriage struct {
Road xml.Name `xml:"http://www.example.com/XMLSchema/standard/2012 road"`
}
func main() {
fr := Carriage{
MainCarriage: SeaCarriage{
LoadFactor: 70,
SeaCargoType: "Container",
},
}
xmlBlob, err := xml.MarshalIndent(&fr, "", "\t")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(xmlBlob))
}
- 1 回答
- 0 关注
- 161 浏览
添加回答
举报