<GetCompetitivePricingForASINResult ASIN="0547569653" status="Success">
<Product xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd"
xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<Identifiers>
<MarketplaceASIN>
<MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
<ASIN>0547569653</ASIN>
</MarketplaceASIN>
</Identifiers>
<CompetitivePricing>
<CompetitivePrices>
<CompetitivePrice belongsToRequester="false" condition="Used" subcondition="Good">
<CompetitivePriceId>2</CompetitivePriceId>
<Price>
<LandedPrice>
<CurrencyCode>USD</CurrencyCode>
<Amount>9.95</Amount>
</LandedPrice>
<ListingPrice>
<CurrencyCode>USD</CurrencyCode>
<Amount>9.95</Amount>
</ListingPrice>
<Shipping>
<CurrencyCode>USD</CurrencyCode>
<Amount>0.00</Amount>
</Shipping>
</Price>
</CompetitivePrice>
</CompetitivePrices>
<NumberOfOfferListings>
<OfferListingCount condition="Any">113</OfferListingCount>
<OfferListingCount condition="Used">72</OfferListingCount>
<OfferListingCount condition="New">41</OfferListingCount>
</NumberOfOfferListings>
</CompetitivePricing>
<SalesRankings>
<SalesRank>
<ProductCategoryId>book_display_on_website</ProductCategoryId>
<Rank>48661</Rank>
</SalesRank>
<SalesRank>
<ProductCategoryId>4209</ProductCategoryId>
<Rank>31</Rank>
</SalesRank>
<SalesRank>
<ProductCategoryId>6511974011</ProductCategoryId>
<Rank>65</Rank>
</SalesRank>
<SalesRank>
<ProductCategoryId>16587</ProductCategoryId>
<Rank>93</Rank>
</SalesRank>
</SalesRankings>
</Product>
</GetCompetitivePricingForASINResult>
我仅在ProductCategoryId等于“book_display_on_website”时才尝试检索“排名”字段,但是,在我当前的尝试中,它似乎将其“排名”设置为最后一个SalesRank条目(93)(应为(48661))。有人可以指出我正确的方向吗?
使用这种解码方法甚至可能吗?还是需要go-pkg-xmlx或gokogiri之类的东西? (我来自php,通常在php上使用simple_xml_parser来处理此类事情。)
type Data struct {
XMLName xml.Name `xml:"GetCompetitivePricingForASINResponse"`
Item []Item `xml:"GetCompetitivePricingForASINResult"`
}
type Item struct {
Pcat string `xml:"Product>SalesRankings>SalesRank>ProductCategoryId"`
ASIN string `xml:"ASIN,attr"`
Rank string `xml:"Product>SalesRankings>SalesRank>Rank"`
}
result, err := api.GetCompetitivePricingForASIN(asins)
if (err != nil) {
fmt.Println(err)
}
data := &Data{}
xml.Unmarshal([]byte(result), data)
if err != nil {
log.Fatal(err)
}
for i := 0; i < len(data.Item); i++ {
fmt.Printf("%s\n", data.Item[i])
}
最佳答案
xml.Unmarshal()
返回一个您不存储也不检查的error
:
xml.Unmarshal([]byte(result), data)
if (err != nil) {
fmt.Println(err)
}
因此,您在下一行中测试的
err
是,而不是,即xml.Unmarshal()
的结果,但与api.GetCompetitivePricingForASIN(asins)
先前返回的值相同。如果修改它以正确存储
Unmarshal()
的结果:err = xml.Unmarshal([]byte(result), data)
您将得到以下错误(包装):
expected element type <GetCompetitivePricingForASINResponse> but have
<GetCompetitivePricingForASINResult>
您的模型没有正确描述XML输入。尝试使用以下结构对XML(您要删除的部分)进行建模:
type Data struct {
ASIN string `xml:"ASIN,attr"`
SalesRanks []SalesRank `xml:"Product>SalesRankings>SalesRank"`
}
type SalesRank struct {
Pcat string `xml:"ProductCategoryId"`
Rank string `xml:"Rank"`
}
使用此模型,您可以打印如下结果:
for _, item := range data.SalesRanks {
fmt.Printf("Cat: %s; Rank: %s\n", item.Pcat, item.Rank)
}
输出:
Cat: book_display_on_website; Rank: 48661
Cat: 4209; Rank: 31
Cat: 6511974011; Rank: 65
Cat: 16587; Rank: 93
在Go Playground上尝试完整的程序。
这是更简单,更有用的打印方法:
fmt.Printf("%+v", data)
输出(包装):
&{ASIN:0547569653 SalesRanks:[{Pcat:book_display_on_website Rank:48661}
{Pcat:4209 Rank:31} {Pcat:6511974011 Rank:65} {Pcat:16587 Rank:93}]}