ruby-on-rails – 如何在rails应用程序中美化xml代码

有没有一种简单的方式来打印一个未形成的xml字符串,以便在rails应用程序中的 ruby中进行屏幕显示?有什么像一个xml美容师?

解决方法

Ruby核心REXML :: Document有漂亮的打印:
REXML::Document#write( output=$stdout,indent=-1,transitive=false,ie_hack=false )

indent: An integer. If -1,no
indenting will be used; otherwise,the
indentation will be twice this number
of spaces,and children will be
indented an additional amount. For a
value of 3,every item will be
indented 3 more levels,or 6 more
spaces (2 * 3). Defaults to -1

一个例子:

require "rexml/document"

doc = REXML::Document.new "<a><b><c>TExt</c><d /></b><b><d/></b></a>"
out = ""
doc.write(out,1)
puts out

生产:

<a>
 <b>
  <c>
   TExt
  </c>
  <d/>
 </b>
 <b>
  <d/>
 </b>
</a>

编辑:Rails已经加载了REXML,因此您只需要生成新的文档,然后将漂亮的打印的XML写入一些字符串,然后将其嵌入到< pre>标签.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...