我试图解组以下XML,但收到错误。
<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<Items>
<Item>
<ASIN>B005XSS8VC</ASIN>
</Item>
</Items>
Run Code Online (Sandbox Code Playgroud)
这是我的结构:
type Product struct {
XMLName xml.Name `xml:"Item"`
ASIN string
}
type Result struct {
XMLName xml.Name `xml:"ItemSearchResponse"`
Products []Product `xml:"Items"`
}
Run Code Online (Sandbox Code Playgroud)
错误的文本是“期望的元素类型<Item>但有<Items>”,但是我看不到哪里出错了。任何帮助表示赞赏。
v := &Result{Products: nil}
err = xml.Unmarshal(xmlBody, v)
Run Code Online (Sandbox Code Playgroud)
这对我有用(请注意Items>Item):
type Result struct {
XMLName xml.Name `xml:"ItemSearchResponse"`
Products []Product `xml:"Items>Item"`
}
type Product struct {
ASIN string `xml:"ASIN"`
}
Run Code Online (Sandbox Code Playgroud)