Skip to content

Instantly share code, notes, and snippets.

@geetfun
Created April 21, 2010 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geetfun/373797 to your computer and use it in GitHub Desktop.
Save geetfun/373797 to your computer and use it in GitHub Desktop.
# Put this file eg. xml_parser.rb into your config/initializers directory, reboot your application
ActiveSupport::XmlMini.backend = "Nokogiri"
module ActiveSupport
module XmlMini_Nokogiri
def parse(data)
if !data.respond_to?(:read)
data = StringIO.new(data || '')
end
responds_to_getc = data.respond_to?(:getc)
if responds_to_getc
char = data.getc
data.ungetc(char)
end
if char.nil? && responds_to_getc
{}
else
doc = Nokogiri::XML(data)
raise doc.errors.first if doc.errors.length > 0
doc.to_hash
end
end
end
end
@geetfun
Copy link
Author

geetfun commented Apr 21, 2010

For the above example, I've used the nokogiri gem for the backend parser, so you may need to add it to your Gemfile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment