John Lam wrote:
> I'm writing some REST-ian code to talk to the Amazon Web Services
> since there are some bugs in SOAP4R (which I'll explain in a future
> mail).
> 
> While manually parsing the XML that is returned from Amazon's REST
> interface, I wound up writing the following code:
> 
> item.details = []
> doc.elements.each('ItemLookupResponse/Items/Item') do |item|
>   # construct an item_detail
>   item_details << item_detail
> end
> 
> This doesn't *feel* right. I wanted to use collect() but it's not
> implemented in REXML. Is there a more Ruby-esque approach to gathering
> some objects into an array while parsing XML?

require 'rexml/document'
doc = REXML::Document.new( "<a><b><c>1</c><c>2</c><c>3</c></b></a>")
p doc.elements.to_a( '/a/b/c' ).collect { |e| e.text  }



James
-- 
http://www.ruby-doc.org
http://www.rubyxml.com
http://catapult.rubyforge.org
http://orbjson.rubyforge.org
http://ooo4r.rubyforge.org
http://www.jamesbritt.com