【发布时间】:2018-11-08 09:24:30
【问题描述】:
我从 Amazon 检索到 2 段非常相似的 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"`
}
除了元素“GetLowestPricedOffersForASINResult”被称为“GetLowestPricedOffersForSKUResult”之外,我还有一些结构相同的 XML 数据。
如果我手动将标签 xml:"GetLowestPricedOffersForASINResult" 更改为 xml:"GetLowestPricedOffersForSKUResult",那么代码将愉快地处理其他 XML。
我想让这个通用,如果你愿意,我的选择似乎是:
复制代码并有 2 个基本相同的大块。这很容易,但在我看来是错误的方法。
通过对 XML 原始字符串进行搜索和替换并简单地将 GetLowestPricedOffersForSKUResult 替换为 GetLowestPricedOffersForASINResult 来解决它,然后代码会愉快地处理数据,这似乎又是错误的。
可能使用反射动态更改结构标记。这可能吗?
有没有人有任何建议来完成 3 或其他想法/方法来处理这个问题?
GetLowestPricedOffersForASINResult 结果集的代码位于go playground(不完整但有效)
【问题讨论】:
-
为你在多个地方需要的结构声明一个命名类型并重用它。 play.golang.org/p/JOzgpjqDwAn
-
上述方法有效,感谢@mkopriva