Forum: Ruby new to Ruby and XML

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5f924d525edb35300456d2ae343e0e2?d=identicon&s=25 Hunter Walker (siguva)
on 2006-05-08 08:30
Hi all,

I have an API call to another database app that returns the XML below:

<records>        <record>          <f id='3'>9</f>          <f
id='8'>ISA</f>        </record>        <record>          <f id='3'>8</f>
<f id='8'>AAA</f>        </record>        <record>          <f
id='3'>1</f>          <f id='8'>AAA</f>        </record>        <record>
<f id='3'>2</f>          <f id='8'>AAB</f>        </record>
<record>          <f id='3'>4</f>          <f id='8'>AAC</f>
</record>        <record>          <f id='3'>5</f>          <f
id='8'>AAD</f>        </record>        <record>          <f id='3'>7</f>
<f id='8'>AAE</f>        </record>        <record>          <f
id='3'>12</f>          <f id='8'>AAF</f>        </record>
<record>          <f id='3'>13</f>          <f id='8'>AAG</f>
</record>      </records>

I'd like to loop through each of these records and create a hash.  The
first value of each record being the key and the second being the value
in the hash.

Any ideas?  Thank you for helping a noob!

-Hunter
A9b6a93b860020caf9d2d1d58c32478f?d=identicon&s=25 Ross Bamford (Guest)
on 2006-05-08 13:56
(Received via mailing list)
On Mon, 2006-05-08 at 15:31 +0900, Hunter Walker wrote:
> </record>        <record>          <f id='3'>5</f>          <f
> id='8'>AAD</f>        </record>        <record>          <f id='3'>7</f>
> <f id='8'>AAE</f>        </record>        <record>          <f
> id='3'>12</f>          <f id='8'>AAF</f>        </record>
> <record>          <f id='3'>13</f>          <f id='8'>AAG</f>
> </record>      </records>
>
> I'd like to loop through each of these records and create a hash.  The
> first value of each record being the key and the second being the value
> in the hash.

If you want to parse the XML you could do it with libxml-ruby
(http://libxml.rubyforge.org/), e.g.

require 'xml/libxml'
d = XML::Parser.string(xml).parse
h = d.find('/records/record').inject({}) do |h,n|
  data = n.find('f').to_a
  h.merge!({data.first.content => data.last.content})
end

However, you don't really need to iterate the records, assuming your XML
is structured just as above:

require 'xml/libxml'
d = XML::Parser.string(xml).parse
h = Hash[*d.find('/records/record/f').map { |n| n.content}]

This translates pretty easily to REXML if you don't have libxml-ruby
installed:

require 'rexml/document'
require 'rexml/xpath'

d = REXML::Document.new(xml)
h = Hash[*REXML::XPath.match(d.root, '/records/record/f').map { |n|
n.get_text }]

(Maybe that can be done better, I don't know REXML too well I'm
afraid).

Hope that helps,
This topic is locked and can not be replied to.