开发者

Yaml'ifying an XML file?

开发者 https://www.devze.com 2022-12-16 12:17 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消