Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml.Unmarshal error: "expected element type <Item> but have <Items>"

Tags:

go

I'm trying to unmarshal the following XML, but am receiving an error.

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

Here are my structs:

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

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

The text of the error is "expected element type <Item> but have <Items>," but I can't see where I'm going wrong. Any help is appreciated.

v := &Result{Products: nil}
err = xml.Unmarshal(xmlBody, v)
like image 319
Matthew H Avatar asked Feb 16 '13 23:02

Matthew H


1 Answers

This works for me (note the Items>Item):

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

type Product struct {
    ASIN   string `xml:"ASIN"`
}
like image 60
Matthew H Avatar answered Oct 05 '22 13:10

Matthew H