在ruby中格式化xml字符串的最佳方法是什么? [英] what's the best way to format an xml string in ruby?
本文介绍了在ruby中格式化xml字符串的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出这样的xml字符串:
given an xml string like this:
<some><nested><xml>value</xml></nested></some>
什么是最好的选择(使用ruby)将其格式化为可读性:
what's the best option (using ruby) to format it into something readable like:
<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写入任何需要<(字符串)的东西,所以这也是有效的:
The #write writes to anything that takes <<(string), so this is valid too:
doc.write( $stdout, 2 )
doc.write( an_open_file, 2 )
这篇关于在ruby中格式化xml字符串的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文