I''ve built a simple service that returns an xml feed like so:
<users>
<user>
<first_name>blah</first_name>
<last_name>blah</last_name>
<id>1</id>
</user>
<user>
<first_name>blah</first_name>
<last_name>blah</last_name>
<id>2</id>
</user>
etc..
</users>
What is a quick way to map this result to an array of user objects?
Thanks
Marlon
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 Moyer
<marlon.moyer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:>
> I''ve built a simple service that returns an xml feed like so:
>
> <users>
> <user>
> <first_name>blah</first_name>
> <last_name>blah</last_name>
> <id>1</id>
> </user>
> <user>
> <first_name>blah</first_name>
> <last_name>blah</last_name>
> <id>2</id>
> </user>
> etc..
> </users>
>
>
> What is a quick way to map this result to an array of user objects?
>
> Thanks
>
> 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"
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 = <your xml>
users = []
Hash.from_xml(xml)[''users''][''user''].each {
|user_vals|
users << User.new(user_vals)
}
b
Marlon Moyer wrote:> 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 <http://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 Moyer*
<marlon.moyer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
> <mailto:marlon.moyer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>>
wrote:
>
> I''ve built a simple service that returns an xml feed like so:
>
> <users>
> <user>
> <first_name>blah</first_name>
> <last_name>blah</last_name>
> <id>1</id>
> </user>
> <user>
> <first_name>blah</first_name>
> <last_name>blah</last_name>
> <id>2</id>
> </user>
> etc..
> </users>
>
>
> What is a quick way to map this result to an array of user objects?
>
> Thanks
>
> 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"
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
I''ve got an almost identical problem, except my data looks like:
<users>
<user id="1">
<first_name="blah"/>
<last_name="blah"/>
</user>
<user id="2">
<first_name="blah"/>
<last_name="blah"/>
</user>
...
</users>
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.
On 20/02/07, Marlon Moyer
<marlon.moyer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> I''ve built a simple service that returns an xml feed like so:
>
> <users>
> <user>
> <first_name>blah</first_name>
> <last_name>blah</last_name>
> <id>1</id>
> </user>
> <user>
> <first_name>blah</first_name>
> <last_name>blah</last_name>
> <id>2</id>
> </user>
> etc..
> </users>
>
>
> What is a quick way to map this result to an array of user objects?
>
> Thanks
>
> Marlon
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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 David Mitchell wrote:> I''ve got an almost identical problem, except my data looks like: > > <users> > <user id="1"> > <first_name="blah"/> > <last_name="blah"/> > </user> > <user id="2"> > <first_name="blah"/> > <last_name="blah"/> > </user> > ... > </users> > > 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. > > On 20/02/07, Marlon Moyer <marlon.moyer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> I''ve built a simple service that returns an xml feed like so: >> >> <users> >> <user> >> <first_name>blah</first_name> >> <last_name>blah</last_name> >> <id>1</id> >> </user> >> <user> >> <first_name>blah</first_name> >> <last_name>blah</last_name> >> <id>2</id> >> </user> >> etc.. >> </users> >> >> >> What is a quick way to map this result to an array of user objects? >> >> Thanks >> >> Marlon >> >> > >> > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Yep, that''s kind of what I suspected. Thanks Dave M. On 22/02/07, Ben Munat <bmunat-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 > > David Mitchell wrote: > > I''ve got an almost identical problem, except my data looks like: > > > > <users> > > <user id="1"> > > <first_name="blah"/> > > <last_name="blah"/> > > </user> > > <user id="2"> > > <first_name="blah"/> > > <last_name="blah"/> > > </user> > > ... > > </users> > > > > 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. > > > > On 20/02/07, Marlon Moyer <marlon.moyer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> I''ve built a simple service that returns an xml feed like so: > >> > >> <users> > >> <user> > >> <first_name>blah</first_name> > >> <last_name>blah</last_name> > >> <id>1</id> > >> </user> > >> <user> > >> <first_name>blah</first_name> > >> <last_name>blah</last_name> > >> <id>2</id> > >> </user> > >> etc.. > >> </users> > >> > >> > >> What is a quick way to map this result to an array of user objects? > >> > >> Thanks > >> > >> Marlon > >> > >> > > >> > > > > > > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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(''
<users>
<user id="1">
<first_name="blah"/>
<last_name="blah"/>
</user>
<user id="2">
<first_name="blah"/>
<last_name="blah"/>
</user>
</users>
''
)
(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 Thomas <mrt-gkTqyYPWbQbz1n+OaKNE4w@public.gmane.org>
wrote:>
>
> 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
>
>
> >
>
--
"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."
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
aktxyz-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Apr-19 14:50 UTC
Re: Mapping xml to active record objects
actually, it is xml the <tagname/> is shorthand for <tagname></tagname> On Feb 22, 3:19 pm, "Mark Thomas" <m...-gkTqyYPWbQbz1n+OaKNE4w@public.gmane.org> wrote:> Um, that''s not evenXML. But if it wereXML(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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---