我从Amazon检索到了两个非常相似的XML片段。
<?xml version="1.0"?><GetLowestPricedOffersForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetLowestPricedOffersForASINResult MarketplaceID="A1F83G8C2ARO7P" ItemCondition="New" ASIN="0195019199" status="Success">
<Identifier>
<MarketplaceId>A1F83G8C2ARO7P</MarketplaceId>
<ASIN>0195019199</ASIN>
<ItemCondition>New</ItemCondition>
<TimeOfOfferChange>2018-11-07T02:05:14.342Z</TimeOfOfferChange>
</Identifier>
<Summary>
<TotalOfferCount>45</TotalOfferCount>
<NumberOfOffers>
<OfferCount condition="used" fulfillmentChannel="Merchant">14</OfferCount>
<OfferCount condition="new" fulfillmentChannel="Amazon">1</OfferCount>
<OfferCount condition="new" fulfillmentChannel="Merchant">30</OfferCount>
</NumberOfOff........ etc xml continues
</GetLowestPricedOffersForASINResult>
<ResponseMetadata>
<RequestId>fef8c86d-c563-4373-81c9-78dcf691283c</RequestId>
</ResponseMetadata>
</GetLowestPricedOffersForASINResponse>
我目前使用自定义类型将其解组,并将自定义解组为如下结构:
type LowestPricedPricedOffers struct {
Error AmazonError `xml:"Error"`
All struct {
/*The only way I found to retrieve 'status' from the GetLowestPricedOffersForASINResult element was to wrap in the struct 'All'.
This is the only reason the All struct exists. Ideally would like to remove*/
Status string `xml:"status,attr"`
ASIN string `xml:"Identifier>ASIN"`
ItemCondition string `xml:"Identifier>ItemCondition"`
TimeOfOfferChange string `xml:"Identifier>TimeOfOfferChange"`
TotalOfferCount int `xml:"Summary>TotalOfferCount"`
ListPrice float32 `xml:"Summary>ListPrice>Amount"`
OfferCount offerCount `xml:"Summary>NumberOfOffers"`
//Want to take Currency code from LowestPrices below but cannot think of a way to achieve this against the lowestPrices type
//CurrencyCode string `xml:"CurrencyCode"`
BuyBoxPrices buyBoxPrices `xml:"Summary>BuyBoxPrices"`
LowestPrices lowestPrices `xml:"Summary>LowestPrices"`
BuyBoxEligibleOffers buyBoxEligibleOffers `xml:"Summary>BuyBoxEligibleOffers"`
Offers []struct {
SubCondition string `xml:"SubCondition"`
SellerPositiveFeedbackRating float32 `xml:"SellerFeedbackRating>SellerPositiveFeedbackRating"`
FeedbackCount int `xml:"SellerFeedbackRating>FeedbackCount"`
ShippingTime struct {
MinimumHours int `xml:"minimumHours,attr"`
MaximumHours int `xml:"maximumHours,attr"`
AvailabilityType string `xml:"availabilityType,attr"`
}
ListingPrice float32 `xml:"ListingPrice>Amount"`
Shipping float32 `xml:"Shipping>Amount"`
ShipsFrom string `xml:"ShipsFrom>Country"`
IsFulfilledByAmazon bool `xml:"IsFulfilledByAmazon"`
IsBuyBoxWinner bool `xml:"IsBuyBoxWinner"`
IsFeaturedMerchant bool `xml:"IsFeaturedMerchant"` //true if the seller of the item is eligible to win the Buy Box.
} `xml:"Offers>Offer"`
} `xml:"GetLowestPricedOffersForASINResult"`
}
我还有一些XML数据,它们的结构是相同的,只是元素"GetLowestPricedOffersForASINResult"被称为"GetLowest PricedOffers ForSKUResult"。
如果我手动将标记xml:"GetLowestPricedOffersForASINResult"
更改为xml:"GetLowestPricedOffersForSKUResult"
,那么代码将愉快地处理其他XML。
我想让这个通用,如果你愿意,我的选择似乎是:
复制代码并有两个基本相同的大块。这很容易,但在我看来是错误的做法。
通过对XML原始字符串进行搜索和替换,并简单地将GetLowestPricedOffersForSKUResult替换为GetLowest普华永道OffersForASINResult,代码就会愉快地处理数据,这似乎也是错误的。
可能使用反射来动态更改结构标记。这可能吗?
有人对在go中完成3或其他想法/方法有什么建议吗?
GetLowestPricedOffersForASINRResult结果集的代码正在进行中(未完成但有效(
根据@mkopriva提供的建议,我制作了一个更新版的go playground脚本。
我不确定我的改变是否完美,也不确定是否准确地反映了他的想法,但总的来说,它似乎满足了问题的要点。
如果有人有任何改进,欢迎他们。