Compress new line character
require 'rexml/document' text = %{<doc><a>Some text</a>\n <b>Some more</b>\n\n} REXML::Document.new(text, { :compress_whitespace => :all }).to_s # => "<doc><a>Some text</a>\n <b>Some more</b>\n</doc>"