On Mar 27, 10:18 am, pepe <p...@betterrpg.com> wrote:
> On Mar 26, 1:24 pm, Gustavo de Sá Carvalho Honorato
>
> <gustavohonor...@gmail.com> wrote:
> > Hi,
>
> > to serialize an array to xml we can use:
>
> > an_array = [ 1, 2, 3]
> > an_array.to_xml
>
> > But to I can't find a way to convert back XML to array. I didn't find a
> > method Array.from_xml like Hash.from_xml.
>
> > Any ideas?
>
> I have never done what you are trying but I know there is a way of
> converting a hash to an array (http://ruby-doc.org/core/classes/
> Hash.html#M000722) so you could do a double conversion. If you need a
> simple array then you could just flatten the resulting array of
> arrays.

I have looked again at this and I actually couldn't find a 'to_xml'
method for Array or for anything else in Ruby. The method exists in
Rails, though but for ActiveRecord instances, though, not for arrays.
If what you are trying to do is converting a model instance to xml and
backthis is how you could do:

# Converting to XML
my_model_instance = MyModel.find my_id
xml = my_model_instance.to_xml

# Generating instance from XML
my_new_model = MyModel.new
my_new_model.from_xml xml

-- 
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@googlegroups.com.
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to