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)
}
}
网友评论