Search code examples
ruby-on-railsxmlamazon-web-servicesrexml

Get same attribute values with rexml


Quick question.

Suppose you have an xml like:

<ItemAttributes>
  <Author>John Green</Author>
  <Author>David Levithan</Author>
  <Binding>Hardcover</Binding>
  <Brand>Dutton Juvenile</Brand>
  <EAN>9780525421580</EAN>
</ItemAttributes>

I am using rexml to parse it. I am stuck while getting the values of the same attribute. When I do:

doc              = REXML::Document.new(xml_data)
doc.elements['ItemAttributes/Author/'].each do |element|
  logger.info(element)
end 

It only give me the first attribute value i.e. John Green. How can I get the second value of the same attribute('Author'). I tried doing '*' also but did not work.

Any thoughts?


Solution

  • Try this:

    doc.elements.each("ItemAttributes/Author") do |author|
      logger.info(author.text)
    end