go – xml.Unmarshal错误:“预期的元素类型但有”

前端之家收集整理的这篇文章主要介绍了go – xml.Unmarshal错误:“预期的元素类型但有”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试解组以下 XML,但收到错误.

<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<Items>
<Item>
<ASIN>B005XSS8VC</ASIN>
</Item>
</Items>

这是我的结构:

type Product struct {
    XMLName xml.Name `xml:"Item"`
    ASIN    string
}

type Result struct {
    XMLName  xml.Name `xml:"ItemSearchResponse"`
    Products []Product `xml:"Items"`
}

错误的文本是“预期的元素类型< Item>但是< Items>”,但我看不出我出错的地方.任何帮助表示赞赏.

v := &Result{Products: nil}
err = xml.Unmarshal(xmlBody,v)

解决方法

这对我有用(请注意Items> Item):

type Result struct {
XMLName       xml.Name `xml:"ItemSearchResponse"`
Products      []Product `xml:"Items>Item"`
}

type Product struct {
    ASIN   string `xml:"ASIN"`
}

猜你在找的XML相关文章