golang解析xml数据
<?xml version="1.0" encoding="utf-8"?>
<AjaxResult>
<state>200</state>
<message>获取成功</message>
<data>admin</data>
</AjaxResult>
构建结构体利用golang的xml.Unmarshal转换
resdata := `<?xml version="1.0" encoding="utf-8"?>
<AjaxResult>
<state>200</state>
<message>获取成功</message>
<data>admin</data>
</AjaxResult>`
type xml2 struct {
State string `xml:"state"`
Message string `xml:"message"`
Data string `xml:"data"`
}
x := xml2{}
err = xml.Unmarshal([]byte(resdata), &x)
if err != nil {
fmt.Println(err.Error())
} else {
fmt.Println(x.Data)
}
//结果 admin