如何在此hpricot对象中找到元素?

时间:2011-02-18 12:48:36

标签: ruby hpricot amazon-product-api

鉴于本文底部的hpricot xml,如何选择“item”而不必使用.each?每一篇文档都使用了

的变体
@res.items.each do |item|
  # do stuff
end

在这种情况下,这是毫无意义的,因为只有一个“项目”。在过去的几年里,为了做到这一点,一直在撕扯头发。

已修改以添加更多信息:

好的,从早期评论来看,我在某个地方错过了这一点,所以我会提供更多信息。

我正在使用名为amazon-ecs的ruby gem来从Amazon检索产品信息。在宝石的网站上,它被描述为

  

使用Hpricot的通用Ruby Amazon产品广告API(以前称为电子商务REST API)。它使用Response和Element包装类来轻松访问REST API XML输出。它是通用的,因此您可以扩展Amazon :: Ecs以轻松支持其他未实现的操作;并且响应对象只包含Hpricot元素对象,而不是向XML元素map提供一对一的对象/属性。

现在要成为hones我真的不明白这意味着什么,但我怀疑有关包装Response对象的一点是什么让这很困难!

基本上,当我这样做时:

@res = Amazon::Ecs.item_lookup(ean, options_hash)

然后我打印出“debug @res”,我得到了下面的内容。

希望有所帮助! 结束修改

Hpricot xml:

<Amazon::Ecs::Response:0xa4449cc @doc=#<Hpricot::Doc 
{xmldecl "<?xml version=\"1.0\" ?>"} 
{elem <itemlookupresponse xmlns="http://webservices.amazon.com/AWSECommerceService/2005-10-05"> 
  {elem <operationrequest> 
    {elem <httpheaders> 
      {emptyelem <header name="UserAgent" value="Ruby">} 
    </HTTPHeaders>} 
    {elem <requestid> "b89bad91-f5a1-4daf-87f2-d309dded35d6" </RequestId>} 
    {elem <arguments> 
      {emptyelem <argument name="Operation" value="ItemLookup">} 
      {emptyelem <argument name="SearchIndex" value="Books">} 
      {emptyelem <argument name="Signature" value="dasdasdsadsadsafdfdsfsdsasadsadsd">} 
      {emptyelem <argument name="ItemId" value="9780307463746">} 
      {emptyelem <argument name="IdType" value="ISBN">} 
      {emptyelem <argument name="AWSAccessKeyId" value="sdasdsadsadsadsadsadd">} 
      {emptyelem <argument name="Timestamp" value="2011-02-17T15:08:09Z">} 
      {emptyelem <argument name="Service" value="AWSECommerceService">} 
    </Arguments>} 
    {elem <requestprocessingtime> "0.0252220000000000" </RequestProcessingTime>} 
  </OperationRequest>} 
  {elem <items> 
    {elem <request> 
      {elem <isvalid> "True" </IsValid>} 
      {elem <itemlookuprequest> 
        {elem <condition> "New" </Condition>} 
        {elem <deliverymethod> "Ship" </DeliveryMethod>} 
        {elem <idtype> "ISBN" </IdType>} 
        {elem <merchantid> "Amazon" </MerchantId>} 
        {elem <offerpage> "1" </OfferPage>} 
        {elem <itemid> "9780307463746" </ItemId>} 
        {elem <responsegroup> "Small" </ResponseGroup>} 
        {elem <reviewpage> "1" </ReviewPage>} 
        {elem <searchindex> "Books" </SearchIndex>} 
      </ItemLookupRequest>} 
    </Request>} 
    {elem <item> 
      {elem <asin> "0307463745" </ASIN>} 
      {elem <detailpageurl> "http://www.amazon.com/Rework-Jason-Fried/dp/0307463745%3FSubscriptionId%3DAKIAIV6GP6CJC3AINUUQ%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0307463745" </DetailPageURL>} 
      {elem <smallimage> 
        {elem <url> "http://ecx.images-amazon.com/images/I/41Qz6afdrLL._SL75_.jpg" </URL>} 
        {elem <height units="pixels"> "75" </Height>} 
        {elem <width units="pixels"> "50" </Width>} 
      </SmallImage>} 
      {elem <mediumimage> 
        {elem <url> "http://ecx.images-amazon.com/images/I/41Qz6afdrLL._SL160_.jpg" </URL>} 
        {elem <height units="pixels"> "160" </Height>} 
        {elem <width units="pixels"> "106" </Width>} 
      </MediumImage>} {elem <largeimage> {elem <url> "http://ecx.images-amazon.com/images/I/41Qz6afdrLL.jpg" </URL>} {elem <height units="pixels"> "500" </Height>} {elem <width units="pixels"> "331" </Width>} </LargeImage>} {elem <imagesets> {elem <imageset category="primary"> {elem <swatchimage> {elem <url> "http://ecx.images-amazon.com/images/I/41Qz6afdrLL._SL30_.jpg" </URL>} {elem <height units="pixels"> "30" </Height>} {elem <width units="pixels"> "20" </Width>} </SwatchImage>} {elem <smallimage> {elem <url> "http://ecx.images-amazon.com/images/I/41Qz6afdrLL._SL75_.jpg" </URL>} {elem <height units="pixels"> "75" </Height>} {elem <width units="pixels"> "50" </Width>} </SmallImage>} {elem <mediumimage> {elem <url> "http://ecx.images-amazon.com/images/I/41Qz6afdrLL._SL160_.jpg" </URL>} {elem <height units="pixels"> "160" </Height>} {elem <width units="pixels"> "106" </Width>} </MediumImage>} {elem <largeimage> {elem <url> "http://ecx.images-amazon.com/images/I/41Qz6afdrLL.jpg" </URL>} {elem <height units="pixels"> "500" </Height>} {elem <width units="pixels"> "331" </Width>} </LargeImage>} </ImageSet>} </ImageSets>}
      {elem <itemattributes> 
        {elem <author> "Jason Fried" </Author>} 
        {elem <author> "David Heinemeier Hansson" </Author>} 
        {elem <manufacturer> "Crown Business" </Manufacturer>} 
        {elem <productgroup> "Book" </ProductGroup>} 
        {elem <title> "Rework" </Title>} 
      </ItemAttributes>} 
    </Item>} 
  </Items>} 
</ItemLookupResponse>}

2 个答案:

答案 0 :(得分:2)

首先,从@res(来自docs)中提取Hpricot对象。

doc = @res.doc

然后,您应该能够使用Hpricot对象:

puts (doc/:item).inner_html

答案 1 :(得分:0)

你可以做这样的事情

item = (doc/:header).first

上面的内容应该是XML文档中的第一个header节点。它没有经过测试,所以我会给它一些测试

相关问题