How do I convert XML into a hash in Rails?

后端 未结 5 1780
北海茫月
北海茫月 2021-01-05 08:28

How do I convert an XML body to a hash in Ruby?

I have an XML body which I\'d like to parse into a hash


    
           


        
5条回答
  •  执笔经年
    2021-01-05 09:31

    ActiveSupport adds a Hash.from_xml, which does the conversion in a single call. Described in another question: https://stackoverflow.com/a/7488299/937595

    Example:

    require 'open-uri'
    remote_xml_file = "https://www.example.com/some_file.xml"
    data = Hash.from_xml(open(remote_xml_file))
    

提交回复
热议问题