Skip to content

Instantly share code, notes, and snippets.

@siannopollo
Created November 18, 2009 20:31
Show Gist options
  • Select an option

  • Save siannopollo/238218 to your computer and use it in GitHub Desktop.

Select an option

Save siannopollo/238218 to your computer and use it in GitHub Desktop.
require 'hpricot'
def formatted_output(node, indent = 0)
output, tag_name = [], node.etag.sub('/', '')
space = ' ' * indent
if node.children
if node.children.size == 1
output << space + node.to_s
else
output << space + tag_name
node.children.each do |child|
output.concat formatted_output child, indent + 2
end
output << space + node.etag
end
else
output << space + tag_name + node.etag
end
output
end
document = Hpricot.parse File.read(Rails.root + "/spec/fixtures/unformatted.xml")
formatted_xml = formatted_output(document.children.first).join("\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment