doumo6356 2015-03-29 01:14
浏览 148

Golang XML Unmarshal值覆盖问题

<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])
    }
  • 写回答

1条回答 默认 最新

  • donglei_0507 2015-03-30 12:33
    关注

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

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line