什么';在ruby中格式化xml字符串的最佳方法是什么?
给定如下xml字符串:什么';在ruby中格式化xml字符串的最佳方法是什么?,xml,ruby,formatting,Xml,Ruby,Formatting,给定如下xml字符串: <some><nested><xml>value</xml></nested></some> 值 最好的选择是什么(使用ruby)将其格式化为可读的格式,如: <some> <nested> <xml>value</xml> </nested> </some> 价值 刚刚注意到有一个缩进选项可以执行此操作。
<some><nested><xml>value</xml></nested></some>
值
最好的选择是什么(使用ruby)将其格式化为可读的格式,如:
<some>
<nested>
<xml>value</xml>
</nested>
</some>
价值
刚刚注意到有一个缩进
选项可以执行此操作。但是也请把你的答案贴出来。并不是每个想这样做的人都使用builder。此外,可能还有一些更快的xml字符串解决方案,这些解决方案不是您自己创建的
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
doc.write( $stdout, 2 )
doc.write( an_open_file, 2 )