Convert a Nokogiri document to a Ruby Hash

Ivan picture Ivan · Aug 5, 2009 · Viewed 45.2k times · Source

Is there an easy way to convert a Nokogiri XML document to a Hash?

Something like Rails' Hash.from_xml.

Answer

Guillaume Roderick picture Guillaume Roderick · Sep 20, 2011

If you want to convert a Nokogiri XML document to a hash, just do the following:

require 'active_support/core_ext/hash/conversions'
hash = Hash.from_xml(nokogiri_document.to_s)