xml.Unmarshal错误:“期望的元素类型为,但实际为

huangapple go评论80阅读模式
英文:

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.

&lt;ItemSearchResponse xmlns=&quot;http://webservices.amazon.com/AWSECommerceService/2011-08-01&quot;&gt;
&lt;Items&gt;
&lt;Item&gt;
&lt;ASIN&gt;B005XSS8VC&lt;/ASIN&gt;
&lt;/Item&gt;
&lt;/Items&gt;

</ItemSearchResponse>

Here are my structs:

type Product struct {
	XMLName xml.Name `xml:&quot;Item&quot;`
	ASIN    string
}

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

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

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

答案1

得分: 6

这对我有用(注意Items&gt;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&gt;Item):

type Result struct {
XMLName       xml.Name `xml:&quot;ItemSearchResponse&quot;`
Products      []Product `xml:&quot;Items&gt;Item&quot;`
}

type Product struct {
	ASIN   string `xml:&quot;ASIN&quot;`
}

答案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 (
	&quot;encoding/xml&quot;
	&quot;log&quot;
)

type Product struct {
	ASIN    string   `xml:&quot;ASIN&quot;`
}
type Items struct {
	Products    []Product `xml:&quot;Item&quot;`
}

type Result struct {
	XMLName  xml.Name `xml:&quot;ItemSearchResponse&quot;`
	Items    Items `xml:&quot;Items&quot;`
}

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

}

it will output:

&amp;{XMLName:{Space:http://webservices.amazon.com/AWSECommerceService/2011-08-01 Local:ItemSearchResponse} Products:{Products:[{ASIN:B005XSS8VC} {ASIN:C005XSS8VC}]}}

huangapple
  • 本文由 发表于 2013年2月17日 07:30:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/14916307.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定