Gustavo de Sá Carvalho Honorato
2011-Mar-26 17:24 UTC
How to deserialize an array from XML
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? Thanks, Gustavo -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 26, 1:24 pm, Gustavo de Sá Carvalho Honorato <gustavohonor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On Mar 27, 10:18 am, pepe <P...-1PhG29ZdMB/g+20BJ0uB2w@public.gmane.org> wrote:> On Mar 26, 1:24 pm, Gustavo de Sá Carvalho Honorato > > <gustavohonor...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.