首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Golang XML解组值覆盖问题

Golang XML解组值覆盖问题
EN

Stack Overflow用户
提问于 2015-03-29 09:14:32
回答 1查看 442关注 0票数 2
代码语言:javascript
复制
<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>

仅当ProductCategoryId等于"book_display_on_website“时,我才尝试检索" Rank”字段,然而,在我当前的尝试中,似乎将其设置为最后一个SalesRank条目(93) (它应该是(48661))。谁能给我指个方向?

使用这种Unmarshal方法可以做到这一点吗?或者像go-pkg-xmlx或gokogiri这样的东西是必需的?(我来自php,通常在php上使用simple_xml_parser来做这类事情。)

代码语言:javascript
复制
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])
    }
EN

回答 1

Stack Overflow用户

发布于 2015-03-30 20:33:41

xml.Unmarshal()返回一个您不存储也不检查的error

代码语言:javascript
复制
xml.Unmarshal([]byte(result), data)
if (err != nil) {
    fmt.Println(err)
}

因此,您在下一行中测试的err,而不是 xml.Unmarshal()的结果,而是与api.GetCompetitivePricingForASIN(asins)之前返回的值相同。

如果您修改它以正确存储Unmarshal()的结果

代码语言:javascript
复制
err = xml.Unmarshal([]byte(result), data)

您将得到以下错误(包装):

代码语言:javascript
复制
expected element type <GetCompetitivePricingForASINResponse> but have
<GetCompetitivePricingForASINResult>

您的模型没有正确描述XML输入。尝试使用以下结构对XML (您想要取出的部分)进行建模:

代码语言:javascript
复制
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"`
}

使用此模型,您可以像这样打印结果:

代码语言:javascript
复制
for _, item := range data.SalesRanks {
    fmt.Printf("Cat: %s; Rank: %s\n", item.Pcat, item.Rank)
}

输出:

代码语言:javascript
复制
Cat: book_display_on_website; Rank: 48661
Cat: 4209; Rank: 31
Cat: 6511974011; Rank: 65
Cat: 16587; Rank: 93

Go Playground上尝试完整的程序。

下面是一种更简单、更具信息量的打印方式:

代码语言:javascript
复制
fmt.Printf("%+v", data)

输出(换行):

代码语言:javascript
复制
&{ASIN:0547569653 SalesRanks:[{Pcat:book_display_on_website Rank:48661}
  {Pcat:4209 Rank:31} {Pcat:6511974011 Rank:65} {Pcat:16587 Rank:93}]}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29324564

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档