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



下面的XML响应是Amazon对其api之一的响应示例。它包括2条记录。我需要弄清楚如何从XML文档中删除记录,如果状态是"ClientError",或者如果它包含一个错误节点……任何一个都可以工作。我在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>

解决方案

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

相关内容

  • 没有找到相关文章

最新更新