Skip to content

Instantly share code, notes, and snippets.

@epugh
Created March 16, 2016 17:47
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 epugh/5f4b40be01aa12ff9f5c to your computer and use it in GitHub Desktop.
Save epugh/5f4b40be01aa12ff9f5c to your computer and use it in GitHub Desktop.
Convert a big blob of XML into pretty printed XML in Ruby/Rails
# Oh dear god this was a pain to figure out! REXML had parsing issues, so instead
# I parse with Nokogiri, then dump it out and feed it to REXML to use the pretty printer.
nokogiri_doc = Nokogiri::XML xml_string
rexml_doc = REXML::Document.new nokogiri_doc.to_xml
formatter = REXML::Formatters::Pretty.new(2)
@doc = ""
formatter.write(rexml_doc, @doc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment