Go xml.Unmarshal错误:";预期元素类型<;项目>;但我们有<;项目>&引用;

Go xml.Unmarshal错误:";预期元素类型<;项目>;但我们有<;项目>&引用;,go,Go,我正在尝试解组以下XML,但收到一个错误 <ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01"> <Items> <Item> <ASIN>B005XSS8VC</ASIN> </Item> </Items> 错误的文本是“预期的元素类型,但有,”但我看不出哪里出错了。感谢您的帮助 v :

我正在尝试解组以下XML,但收到一个错误

<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<Items>
<Item>
<ASIN>B005XSS8VC</ASIN>
</Item>
</Items>
错误的文本是“预期的元素类型
,但有
,”但我看不出哪里出错了。感谢您的帮助

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

结构的结构与xml结构不匹配,下面是一个工作代码:

package main

import (
    "encoding/xml"
    "log"
)

type Product struct {
    ASIN    string   `xml:"ASIN"`
}
type Items struct {
    Products    []Product `xml:"Item"`
}

type Result struct {
    XMLName  xml.Name `xml:"ItemSearchResponse"`
    Items    Items `xml:"Items"`
}

func main() {
    xmlBody := `<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<Items>
<Item>
<ASIN>B005XSS8VC</ASIN>
</Item>
<Item>
<ASIN>C005XSS8VC</ASIN>
</Item>
</Items>`
    v := &Result{}
    err := xml.Unmarshal([]byte(xmlBody), v)
    log.Println(err)
    log.Printf("%+v", v)

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


我认为
xml:…
标记的全部要点是结构的结构不需要与xml的结构匹配。但这是有道理的,并解释了我下面的解决方案工作的原因(因为使用Items>Item“跳过”Items结构)。
package main

import (
    "encoding/xml"
    "log"
)

type Product struct {
    ASIN    string   `xml:"ASIN"`
}
type Items struct {
    Products    []Product `xml:"Item"`
}

type Result struct {
    XMLName  xml.Name `xml:"ItemSearchResponse"`
    Items    Items `xml:"Items"`
}

func main() {
    xmlBody := `<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<Items>
<Item>
<ASIN>B005XSS8VC</ASIN>
</Item>
<Item>
<ASIN>C005XSS8VC</ASIN>
</Item>
</Items>`
    v := &Result{}
    err := xml.Unmarshal([]byte(xmlBody), v)
    log.Println(err)
    log.Printf("%+v", v)

}
&{XMLName:{Space:http://webservices.amazon.com/AWSECommerceService/2011-08-01 Local:ItemSearchResponse} Products:{Products:[{ASIN:B005XSS8VC} {ASIN:C005XSS8VC}]}}
type Result struct {
XMLName       xml.Name `xml:"ItemSearchResponse"`
Products      []Product `xml:"Items>Item"`
}

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