6
给出这样一个XML字符串:在ruby中格式化xml字符串的最佳方式是什么?
<some><nested><xml>value</xml></nested></some>
什么是最好的选择(使用红宝石)把它格式化为喜欢的东西可读:
<some>
<nested>
<xml>value</xml>
</nested>
</some>
给出这样一个XML字符串:在ruby中格式化xml字符串的最佳方式是什么?
<some><nested><xml>value</xml></nested></some>
什么是最好的选择(使用红宝石)把它格式化为喜欢的东西可读:
<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)
只注意到builder有一个indent
选项来做到这一点。但请张贴您的答案。不是每个想要这样做的人都使用建筑师。对于您自己没有创建的xml字符串,也可能有更快的解决方案。