什么是在ruby中格式化xml字符串的最佳方法?

rub*_*iii 6 ruby xml formatting

给出一个像这样的xml字符串:

<some><nested><xml>value</xml></nested></some>
Run Code Online (Sandbox Code Playgroud)

什么是最好的选择(使用ruby)将其格式化为可读的东西:

<some>
  <nested>
    <xml>value</xml>
  </nested>
</some>
Run Code Online (Sandbox Code Playgroud)

ste*_*lag 9

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
Run Code Online (Sandbox Code Playgroud)

#write写入任何带<<(字符串)的东西,所以这也是有效的:

doc.write( $stdout, 2 )
doc.write( an_open_file, 2 )
Run Code Online (Sandbox Code Playgroud)