Mapping xml to active record objects

I’ve built a simple service that returns an xml feed like so:

blah blah 1 blah blah 2 etc..

What is a quick way to map this result to an array of user objects?

Thanks

Marlon

Just to answer my own question: Hpricot!

Very, very easy to do this now. Here’s an example.

doc = HJpricot(open(“http://locahost:3000/user/list”))
(doc/:user).each do |user|
u = User.new
u.id = (user/:login).innerHTML
u.first_name = (user:/:first_name).innerHTML
users.push(u)
end

This has got to be the easiest thing I’ve ever done to consume XML.

On 2/19/07, Marlon M. [email protected] wrote:

<first_name>blah</first_name>

Marlon


“They put a hot wire to my head
'Cause of the things I did and said
And made these feelings go away
Model citizen in every way”

Here’s another option… If your xml elements match your User model’s
attribute names, you can just convert the xml to a hash and loop through
its contents:

xml =
users = []
Hash.from_xml(xml)[‘users’][‘user’].each { |user_vals|
users << User.new(user_vals)
}

b

I’ve got an almost identical problem, except my data looks like:

...

I’m really struggling with Hpricot - my brain just isn’t working today
(probably because of the aircon problems at work today…). If
someone could show me how to map this to an array of User objects,
I’d greatly appreciate it.

Thanks in advance

Dave M.

Sorry, I think the quick and easy stops once you start having attributes
on your elements (or, have elements that don’t match your user’s
fields).

Take a look at ReXML:

http://www.germane-software.com/software/rexml/docs/tutorial.html

Comes with standard with Ruby, IIRC.

b

Um, that’s not even XML. But if it were XML (e.g. <first_name>blah</
first_name>) you could do something like this:

require ‘rubygems’ # if installed via Gems

require ‘xml/libxml’
doc = XML::Document.file(‘users.xml’)

doc.find(’//user’).each do |user|
u = User.new
u.id = user.find_first(’@id’).content
u.first_name = user.find_first(‘first_name’).content
u.last_name = user.find_first(‘last_name’).content
users.push(u)
end

Yep, that’s kind of what I suspected.

Thanks

Dave M.

actually, it is xml

the is shorthand for

yep, that’s what I was thinking about the format, but given that feed,
you
could also do something ugly like this:

require ‘rubygems’
require ‘hpricot’

doc = Hpricot(’


<first_name=“blah”/>
<last_name=“blah”/>


<first_name=“blah”/>
<last_name=“blah”/>



)

(doc/“user”).each do |user|
puts “user id = #{user.attributes[‘id’]}”
puts “first_name = #{user.children
[0].to_s.scan(/first_name=”([^"]*)"/).to_s}"
end

On 2/22/07, Mark T. [email protected] wrote:

u = User.new
u.id         = user.find_first('@id').content
u.first_name = user.find_first('first_name').content
u.last_name  = user.find_first('last_name').content
users.push(u)

end


“We all had delusions in our heads,
we all had our minds made up for us,
we had to believe in something,
…so we did.”

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs