给出一个像这样的xml字符串:
- <some><nested><xml>value</xml></nested></some>
什么是最好的选择(使用ruby)将其格式化为可读的东西:
- <some>
- <nested>
- <xml>value</xml>
- </nested>
- </some>
解决方法
- 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
#write写入任何带<<(字符串)的东西,所以这也是有效的:
- doc.write( $stdout,2 )
- doc.write( an_open_file,2 )