加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 23:06:58 所属栏目:百科 来源:网络整理
导读:我正在尝试解组以下 XML,但收到错误. ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01"ItemsItemASINB005XSS8VC/ASIN/Item/Items 这是我的结构: type Product struct { XMLName xml.Name `xml:"Item"` ASIN strin
我正在尝试解组以下 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"`
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读