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

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

value 

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

   value   

 require "rexml/document" include REXML source ='value' doc = Document.new( source ) doc.write( targetstr = "", 2 ) #indents with 2 spaces puts targetstr 

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

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

只是注意到构建器有一个indent选项来执行此操作。 但也请发布你的答案。 不是每个想要这样做的人都使用构建器。 您也可能没有自己创建的xml字符串的更快解决方案。