鉴于这篇文章底部的hpricot xml,我将如何选择“item”而不必使用.each?每一份文件使用的变化如何查找此hpricot对象中的元素?
@res.items.each do |item|
# do stuff
end
在这种情况下,这是没有意义的,因为只有一个“项目”。在过去的几个世纪中,我们一直在努力解决这个问题。
编辑以添加更多的信息:
好了,所以从早期的意见来看,我缺少点什么地方,所以我会提供更多的信息。
我使用名为amazon-ecs的红宝石宝石从亚马逊检索产品信息。在创业板的网站它被描述为
A generic Ruby Amazon Product Advertising API (previously known as E-commerce REST API) using Hpricot. It uses Response and Element wrapper classes for easy access to the REST API XML output. It is generic, so you can extend Amazon::Ecs to support the other not-implemented operations easily; and the response object just wraps around Hpricot element object, instead of providing one-to-one object/attributes to XML elements map.
我们是珩磨我真的不明白是什么意思,但我怀疑有关包装响应对象位是什么使这个困难!
基本上,当我这样做:
@res = Amazon::Ecs.item_lookup(ean, options_hash)
,然后打印出“调试@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://rads.stackoverflow.com/amzn/click/0307463745" </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>}
这不是一个XML文档。这是一个打印的Hpricot对象。你是想逐字解析这个混乱,还是先重新创建Hpricot对象? – 2011-02-18 13:00:10
好吧,看起来我很迷茫,我在这里做了什么,所以我编辑了OP来尝试并提供更多信息。 – 2011-02-18 13:22:39