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

On Mar 26, 1:24pm, Gustavo de S Carvalho H.
[email protected] wrote:

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.

On Mar 27, 10:18am, pepe [email protected] wrote:

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

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