加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:44:06 所属栏目:百科 来源:网络整理
导读:有没有一种简单的方式来打印一个未形成的xml字符串,以便在rails应用程序中的 ruby中进行屏幕显示?有什么像一个xml美容师? 解决方法 Ruby核心REXML :: Document有漂亮的打印: REXML::Document#write( output=$stdout,indent=-1,transitive=false,ie_hack=f
有没有一种简单的方式来打印一个未形成的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>标签.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读