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

前端之家收集整理的这篇文章主要介绍了什么是在ruby中格式化xml字符串的最佳方法?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
给出一个像这样的xml字符串:
  1. <some><nested><xml>value</xml></nested></some>

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

  1. <some>
  2. <nested>
  3. <xml>value</xml>
  4. </nested>
  5. </some>

解决方法

  1. require "rexml/document"
  2. include REXML
  3.  
  4. source ='<some><nested><xml>value</xml></nested></some>'
  5. doc = Document.new( source )
  6. doc.write( targetstr = "",2 ) #indents with 2 spaces
  7. puts targetstr

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

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

猜你在找的Ruby相关文章