Given the hpricot xml at the bottom of this post, how would I select the "item" without having to use .each? Every single piece of documentation uses a variation of
@res.items.each do |item|
# do stuff
end
Which is pointless in this case because there is only ever one "item". Been tearing y hair out for the last ages trying to get this right.
Edited to add more information:
Ok so judging from the early comments, I'm missing the point somewhere so I'll provide more information.
I'm using a ruby gem called amazon-ecs to retrieve product information from Amazon. On the gem's site it is described as
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.
Now to be hones I don't really understand what that means but I suspect the bit about the wrapping Response object is what's making this difficult!
Basically, when I do this:
@res = Amazon::Ecs.item_lookup(ean, options_hash)
and then I print out "debug @res", I get what I have below.
Hope that helps! End edit
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>开发者_StackOverflow} </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>}
First, extract the Hpricot object from @res (from the docs).
doc = @res.doc
Then, you should be able to use the Hpricot object:
puts (doc/:item).inner_html
You could do something like this
item = (doc/:header).first
The above should get you the first header
node in the XML document. Its not tested so I would give it some testing
精彩评论