what's the best way to format an xml string in ruby?

formatting, ruby, xml

Solution

require "rexml/document"
include REXML

source ='<some><nested><xml>value</xml></nested></some>'
doc = Document.new( source )
doc.write( targetstr = "", 2 ) #indents with 2 spaces
puts targetstr

The #write writes to anything that takes <<(string), so this is valid too:

doc.write( $stdout, 2 )
doc.write( an_open_file, 2 )

Problem

given an xml string like this: ``` <some><nested><xml>value</xml></nested></some> ``` what's the best option (using ruby) to format it into something readable like: ``` <some> <nested> <xml>value</xml> </nested> </some> ```

Original source