1 回答
TA贡献1155条经验 获得超0个赞
package main
import (
"encoding/xml"
"fmt"
)
const data = `<?xml version="1.0" encoding="UTF-8"?>
<users>
<user type="admin">
<name>Elliot</name>
<social>
<facebook>https://facebook.com</facebook>
</social>
</user>
<user type="reader">
<name>Fraser</name>
<social>
<facebook>https://facebook.com</facebook>
</social>
</user>
</users>
`
type Users struct {
Users []*User `xml:"user"`
}
type User struct {
Name string `xml:"name"`
}
func main() {
var users Users
if err := xml.Unmarshal([]byte(data), &users); err != nil {
panic(err)
}
for _, user := range users.Users {
fmt.Println(user.Name)
}
}
https://play.golang.org/p/m65Eio6xskS
- 1 回答
- 0 关注
- 169 浏览
添加回答
举报