Convert array of object to xml

  • Thread starter Shyam Krishna Khadka
  • Start date
S

Shyam Krishna Khadka

Hi all,

I have an array containing objects of different classes.Each class is
basically hash containing attribute/value pairs. Now how can we convert
it to xml format?

Any suggestions will be appreciated.
 
R

Robert Dober

On Tue, Aug 24, 2010 at 6:33 AM, Shyam Krishna Khadka

Hmm I have to do some guessing here

the base idea is that you need hashes as elements, thus parting from
the hypothetical problem

[ :a, 42, "a"].to_xml
RuntimeError: Not all elements respond to to_xml

you can do something like this

[ :a, 42, "xxx"].map{|x| { :name => x } }.to_xml
=> "<?xml version="1.0" encoding="UTF-8"?>\n<records type="array">\n
<record>\n <name type="symbol">a</name>\n </record>\n <record>\n
<name type="integer">42</name>\n </record>\n <record>\n
<name>xxx</name>\n </record>\n</records>\n"

If you still have problems it would be helpful to post some code, one
would need to look into the ary

HTH
R.
 
S

Shyam Krishna Khadka

Robert said:
On Tue, Aug 24, 2010 at 6:33 AM, Shyam Krishna Khadka

Hmm I have to do some guessing here

the base idea is that you need hashes as elements, thus parting from
the hypothetical problem

[ :a, 42, "a"].to_xml
RuntimeError: Not all elements respond to to_xml

you can do something like this

[ :a, 42, "xxx"].map{|x| { :name => x } }.to_xml
=> "<?xml version="1.0" encoding="UTF-8"?>\n<records type="array">\n
<record>\n <name type="symbol">a</name>\n </record>\n <record>\n
<name type="integer">42</name>\n </record>\n <record>\n
<name>xxx</name>\n </record>\n</records>\n"

If you still have problems it would be helpful to post some code, one
would need to look into the ary

HTH
R.



Thanks for your reply.
But I am afraid that it works only in rails console, not in irb console.
This is because to_xml method is defined inside ActiveSupport of rails.
But I want pure ruby code to convert to xml.

Actually in my case, array is the collection of objects. And object is
basically collection of hashes. Finally I made it own using REXML.
For reference see :
http://www.ibm.com/developerworks/xml/tutorials/x-rubyonrailsxml/section4.html
 
R

Robert Dober

On Wed, Aug 25, 2010 at 5:26 AM, Shyam Krishna Khadka
I used rc for my convenience, but why not use ActiveSupport without rails?
REXML might or might not be the best choice for your problem...

Cheers
R.
 
S

Shyam Krishna Khadka

Robert Dober wrote:


I agree with you. But for my case, I am writing pure ruby gem that has
to convert an array of different objects into xml. So using
ActiveSupport inside ruby code seemed to be weird one.

For now, REXML is appropriate for me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top