<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>
I am trying to retrieve the "Rank" field only when the ProductCategoryId is equal to "book_display_on_website", however, in my current attempt it appears to set it Rank to the last SalesRank Entry (93) (it should be (48661)). Can someone point me in the right direction?
Is this even possible using this Unmarshal method? or is something like go-pkg-xmlx or gokogiri required? (I am coming from php and usually use simple_xml_parser on php for this type of stuff.)
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
", data.Item[i])
}
xml.Unmarshal()
returns an error
which you don't store and don't examine:
xml.Unmarshal([]byte(result), data)
if (err != nil) {
fmt.Println(err)
}
So the err
you test in the next line is not the result of the xml.Unmarshal()
but the same value returned previously by api.GetCompetitivePricingForASIN(asins)
.
If you modify it to properly store the result of Unmarshal()
:
err = xml.Unmarshal([]byte(result), data)
You will get the following error (wrapped):
expected element type <GetCompetitivePricingForASINResponse> but have
<GetCompetitivePricingForASINResult>
Your model doesn't properly describe the XML input. Try the following structure to model the XML (the part you want to take out):
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"`
}
Using this model, you can print the results like this:
for _, item := range data.SalesRanks {
fmt.Printf("Cat: %s; Rank: %s
", item.Pcat, item.Rank)
}
Output:
Cat: book_display_on_website; Rank: 48661
Cat: 4209; Rank: 31
Cat: 6511974011; Rank: 65
Cat: 16587; Rank: 93
Try the complete program on the Go Playground.
Here is even a more simple and more informative printing:
fmt.Printf("%+v", data)
Output (wrapped):
&{ASIN:0547569653 SalesRanks:[{Pcat:book_display_on_website Rank:48661}
{Pcat:4209 Rank:31} {Pcat:6511974011 Rank:65} {Pcat:16587 Rank:93}]}