2011-02-18 23 views
0

鉴于这篇文章底部的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>} 
+0

这不是一个XML文档。这是一个打印的Hpricot对象。你是想逐字解析这个混乱,还是先重新创建Hpricot对象? – 2011-02-18 13:00:10

+0

好吧,看起来我很迷茫,我在这里做了什么,所以我编辑了OP来尝试并提供更多信息。 – 2011-02-18 13:22:39

回答

2

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

doc = @res.doc 

然后,你应该能够使用角度来说,Hpricot对象:

puts (doc/:item).inner_html 
0

你可以做这样的事情

item = (doc/:header).first

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

+0

是的,这也是我的想法,但我得到了一个N​​oMethodError:“未定义的方法`/'为#<亚马逊:: Ecs :: Response:0xade2e84>”,我猜是因为它被封装在亚马逊响应或什么? – 2011-02-18 13:03:12