Module: TextUtils::XmlHelper

Defined in:
lib/textutils/helper/xml_helper.rb

Instance Method Summary collapse

Instance Method Details

#prettify_xml(xml) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/textutils/helper/xml_helper.rb', line 7

def prettify_xml( xml )
  require 'rexml/document'
  
  begin
    d = REXML::Document.new( xml )
  
    # d.write( pretty_xml="", 2 )
    # pretty_xml  # return prettified xml
  
    formatter = REXML::Formatters::Pretty.new( 2 )  # indent=2
    formatter.compact = true # This is the magic line that does what you need!
    pretty_xml = formatter.write( d.root, "" )  # todo/checl: what's 2nd arg used for ??
    pretty_xml
  rescue Exception => ex
    "warn: prettify_xml failed: #{ex}\n\n\n" + xml
  end
end