英文:
xml.Unmarshal error: "expected element type <Item> but have <Items>"
问题
我试图解析以下XML,但是收到了一个错误。
<ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01">
<Items>
<Item>
<ASIN>B005XSS8VC</ASIN>
</Item>
</Items>
</ItemSearchResponse>
这是我的结构体:
type Product struct {
XMLName xml.Name `xml:"Item"`
ASIN string
}
type Result struct {
XMLName xml.Name `xml:"ItemSearchResponse"`
Products []Product `xml:"Items"`
}
错误的文本是“expected element type <Item>
but have <Items>
,”,但是我看不出我错在哪里。感谢任何帮助。
v := &Result{Products: nil}
err = xml.Unmarshal(xmlBody, v)
英文:
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>
</ItemSearchResponse>
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)
答案1
得分: 6
这对我有用(注意Items>Item
):
type Result struct {
XMLName xml.Name `xml:"ItemSearchResponse"`
Products []Product `xml:"Items>Item"`
}
type Product struct {
ASIN string `xml:"ASIN"`
}
英文:
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"`
}
答案2
得分: 2
结构体的结构与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)
}
它将输出:
&{XMLName:{Space:http://webservices.amazon.com/AWSECommerceService/2011-08-01 Local:ItemSearchResponse} Products:{Products:[{ASIN:B005XSS8VC} {ASIN:C005XSS8VC}]}}
英文:
The structure of the struct doesn't match with the xml structure, here is a working code:
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)
}
it will output:
&{XMLName:{Space:http://webservices.amazon.com/AWSECommerceService/2011-08-01 Local:ItemSearchResponse} Products:{Products:[{ASIN:B005XSS8VC} {ASIN:C005XSS8VC}]}}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论