如何美化rails应用中的xml代码 [英] How to beautify xml code in rails application

查看:53
本文介绍了如何美化rails应用中的xml代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种简单的方法可以打印未格式化的 xml 字符串以在 ruby​​ on rails 应用程序中进行筛选?类似于 xml 美化器?

Is there a simple way to print an unformated xml string to screen in a ruby on rails application? Something like a xml beautifier?

推荐答案

Ruby 核心 REXML::Document 有漂亮的打印:

Ruby core REXML::Document has pretty printing:

REXML::Document#write( output=$stdout, indent=-1, transitive=false, ie_hack=false )

<块引用>

缩进:一个整数.如果 -1,则没有将使用缩进;否则,缩进将是这个数字的两倍的空间,孩子们将缩进了额外的金额.为一个值为 3,每个项目都将是缩进 3 级,或 6 级空格 (2 * 3).默认为 -1

示例:

An example:

产生:

Produces: 

Rails 已经加载了 REXML,因此您只需生成新文档,然后将漂亮的打印 XML 写入某个字符串,然后可以将其嵌入

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

EDIT: Rails has already REXML loaded, so you only have to produce new document and then write the pretty printed XML to some string which then can be embedded in a <pre> tag.

这篇关于如何美化rails应用中的xml代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆