如何使用Nokogiri从XML响应中删除记录

时间:2015-07-26 04:58:05

标签: ruby-on-rails ruby xml nokogiri

下面的XML响应是亚马逊对其中一个API的响应示例。它包括2条记录。我需要弄清楚如果状态是“ClientError”,或者如果它包含一个Error节点,如何从XML文档中删除记录。任何一个都可以工作。我在Rails项目中使用Nokogiri。如果存在这些条件之一,如何从XML文档中删除它的任何想法?

<?xml version="1.0"?>
<GetMyPriceForASINResponse
  xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">
<GetCompetitivePricingForASINResult ASIN="1933893445" status="ClientError">
  <Error>
    <Type>Sender</Type>
    <Code>InvalidParameterValue</Code>
    <Message>ASIN 1933893445 is not valid for marketplace ATPOWSJGJFNC</Message>
  </Error>
</GetCompetitivePricingForASINResult>
<GetMyPriceForASINResult ASIN="1933890517" status="Success">
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01"
           xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
    <Identifiers>
      <MarketplaceASIN>
        <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
        <ASIN>1933890517</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <Offers>
      <Offer>
        <BuyingPrice>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>303.99</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>300.00</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>3.99</Amount>
          </Shipping>
        </BuyingPrice>
        <RegularPrice>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>300.00</Amount>
        </RegularPrice>
        <FulfillmentChannel>MERCHANT</FulfillmentChannel>
        <ItemCondition>Used</ItemCondition>
        <ItemSubCondition>Acceptable</ItemSubCondition>
        <SellerId>A1IMEXAMPLEWRC</SellerId>
        <SellerSKU>SKU2468</SellerSKU>
      </Offer>
    </Offers>
  </Product>
</GetMyPriceForASINResult>
<ResponseMetadata>
  <RequestId>a3381684-87bd-416e-9b95-EXAMPLE9c236</RequestId>
</ResponseMetadata>
</GetMyPriceForASINResponse>

1 个答案:

答案 0 :(得分:0)

<强>解决方案

# xml is initialized to XML string given in the question

doc  = Nokogiri::XML(xml)
doc.search("[status='ClientError']").remove
puts doc.to_xml

<强>输出

<?xml version="1.0"?>
<GetMyPriceForASINResponse xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01">

<GetMyPriceForASINResult ASIN="1933890517" status="Success">
  <Product xmlns="http://mws.amazonservices.com/schema/Products/2011-10-01" xmlns:ns2="http://mws.amazonservices.com/schema/Products/2011-10-01/default.xsd">
    <Identifiers>
      <MarketplaceASIN>
        <MarketplaceId>ATVPDKIKX0DER</MarketplaceId>
        <ASIN>1933890517</ASIN>
      </MarketplaceASIN>
    </Identifiers>
    <Offers>
      <Offer>
        <BuyingPrice>
          <LandedPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>303.99</Amount>
          </LandedPrice>
          <ListingPrice>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>300.00</Amount>
          </ListingPrice>
          <Shipping>
            <CurrencyCode>USD</CurrencyCode>
            <Amount>3.99</Amount>
          </Shipping>
        </BuyingPrice>
        <RegularPrice>
          <CurrencyCode>USD</CurrencyCode>
          <Amount>300.00</Amount>
        </RegularPrice>
        <FulfillmentChannel>MERCHANT</FulfillmentChannel>
        <ItemCondition>Used</ItemCondition>
        <ItemSubCondition>Acceptable</ItemSubCondition>
        <SellerId>A1IMEXAMPLEWRC</SellerId>
        <SellerSKU>SKU2468</SellerSKU>
      </Offer>
    </Offers>
  </Product>
</GetMyPriceForASINResult>
<ResponseMetadata>
  <RequestId>a3381684-87bd-416e-9b95-EXAMPLE9c236</RequestId>
</ResponseMetadata>
</GetMyPriceForASINResponse>