【代码片段】Go解析Xml

Go语言 小铁匠 2021-09-17

【代码片段】Go解析Xml

Xml格式
<lypeng>
<result>200</result>
<message>用心工作,快乐生活</message>
<desc>小铁匠</desc>
</lypeng>
解析
type XmlResult struct {
    Lypeng xml.Name `xml:"lypeng"`
    Result int64 `xml:"result"`
    Message string `xml:"message"`
    Desc string `xml:"desc"`
}

xmlString := 'xml内容'

xmlInfo := XmlResult{}
err = xml.Unmarshal([]byte(xmlString), &xmlInfo)

如果是接口请求返回xml格式

// '接口请求返回内容'
var response *http.Response 

xmlInfo := XmlResult{}
byteInfo, _ := ioutil.ReadAll(response.Body)
err = xml.Unmarshal(byteInfo, &xmlInfo)

结束

------ 本文结束 感谢阅读 ------