golang解析xml到struct
func Xml() {
xmlDoc := `<?xml version="1.0" encoding="UTF-8"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>`
type xmlStruct struct {
XMLName xml.Name `xml:"note"`
To string `xml:"to"`
From string `xml:"from"`
Heading string `xml:"heading"`
Body string `xml:"body"`
}
x := xmlStruct{}
err := xml.Unmarshal([]byte(xmlDoc), &x)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(x.Body)
}
}
有疑问加站长微信联系(非本文作者)