Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';在ruby中格式化xml字符串的最佳方法是什么?_Xml_Ruby_Formatting - Fatal编程技术网

什么';在ruby中格式化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> 价值 刚刚注意到有一个缩进选项可以执行此操作。

给定如下xml字符串:

<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 )