tags:

views:

51

answers:

2

I want to take an XML file (example is http://xboxapi.duncanmackenzie.net/gamertag.ashx?Gamertag=xpaulbettsx) and just turn it into a Ruby dictionary / ExpandoObject type thing, so I can do something like

gamertag_info = HowDoIDoThisPart.load("Example.xml")
puts gamertag_info.zone
>>> "Underground"
puts gamertag_info.recentgames
>>> <Array output>

I know that there's an easy way to do this - any clues?

+2  A: 

I use the Cobra vs Mongoose library. From the docs:

require 'cobravsmongoose'
xml = '<alice><bob>charlie</bob><bob>david</bob></alice>'
CobraVsMongoose.xml_to_hash(xml)
# => { "alice" => { "bob" => [{ "$" => "charlie" }, { "$" => "david" }] } }
kejadlen
That's very handy!
Paul Betts
+1  A: 

I don't know if you're working with Rails, but ActiveSupport already includes this functionality. Plus then you have access to all the YAML tools as well.

floyd