Yaml'ifying an XML file?
I want 开发者_StackOverflow社区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?
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" }] } }
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.
精彩评论