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

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如何在rails应用程序中美化xml代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有一种简单的方式来打印一个未形成的xml字符串,以便在rails应用程序中的 ruby中进行屏幕显示?有什么像一个xml美容师?

解决方法

Ruby核心REXML :: Document有漂亮的打印:
  1. 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

一个例子:

  1. require "rexml/document"
  2.  
  3. doc = REXML::Document.new "<a><b><c>TExt</c><d /></b><b><d/></b></a>"
  4. out = ""
  5. doc.write(out,1)
  6. puts out

生产:

  1. <a>
  2. <b>
  3. <c>
  4. TExt
  5. </c>
  6. <d/>
  7. </b>
  8. <b>
  9. <d/>
  10. </b>
  11. </a>

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

猜你在找的Ruby相关文章