1 回答
TA贡献1785条经验 获得超4个赞
只需将xml:",chardata"标签添加到FirstName字段即可:
package main
import (
"encoding/xml"
"fmt"
"os"
)
func main() {
type Person struct {
XMLName xml.Name `xml:"person"`
ID int `xml:"id,attr"`
FirstName string `xml:",chardata"`
}
a := &Person{
ID: 13,
FirstName: "John",
}
b := &Person{
ID: 14,
FirstName: "Doe",
}
x := []*Person{}
x = append(x, a)
x = append(x, b)
enc := xml.NewEncoder(os.Stdout)
enc.Indent(" ", " ")
if err := enc.Encode(x); err != nil {
fmt.Printf("error: %v\n", err)
}
}
输出:
<person id="13">John</person>
<person id="14">Doe</person>
- 1 回答
- 0 关注
- 107 浏览
添加回答
举报