Golang XML Unmarshal值覆盖问题

时间:2015-03-29 01:14:32

标签: xml go

<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>

我正在尝试检索&#34; Rank&#34;仅当ProductCategoryId等于&#34; book_display_on_website&#34;时才会生成字段,但是,在我当前的尝试中,它似乎将Rank设置为最后一个SalesRank条目(93)(应该是(48661))。有人能指出我正确的方向吗?

使用这种Unmarshal方法甚至可以实现吗?或者像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])
    }

1 个答案:

答案 0 :(得分:2)

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}]}