没有精美印刷的Rails XML构建器(即缩小的XML) [英] Rails XML builder with no pretty-printing (i.e. minified XML)
问题描述
我正在使用Builder :: XmlMarkup为RESTful API服务器生成XML格式的数据结构。
I am using Builder::XmlMarkup to produce data structures in XML format for a RESTful API server.
最近,我发现了一个错误,该错误来自于漂亮的打印Builder :: XmlMarkup产生了一个充满空白文本的元素,而不是应有的空白元素。
Recently, I discovered a bug where the pretty-printing from Builder::XmlMarkup produced an element full of whitespace text instead of an empty element as it should.
例如,这段代码:
xml.outertag do
xml.list do
# Some code which loops through a list
end
end
正在产生:
<outertag>
<list>
</list>
</outertag>
当内部列表为空列表时,该元素必须为空。 < list />或< list>< / list>。但是,实际的XML是一个< list>标记,其中充满了换行符和其他空格。
When the inner list is an empty list, the element must be empty—i.e. <list/> or <list></list>. However the actual XML is a <list> tag filled with a newline and other whitespace.
那么,如何完全消除Builder的精美打印?目前,我正在考虑使用猴子补丁Builder :: XmlMarkup,以便初始化忽略:indent 参数;尽管我也在考虑使用after_filter。
So, how can I eliminate Builder pretty-printing altogether? Currently, I am thinking of monkey-patching Builder::XmlMarkup so that initialize ignores the :indent parameters; although I'm considering an after_filter as well.
推荐答案
调用 Builder :: XmlMarkup.new 完全没有缩进参数的code>不应给您任何空格。
Calling Builder::XmlMarkup.new
without any indent parameter at all shouldn’t give you any whitespace.
xml = Builder::XmlMarkup.new
xml.outertag do
xml.list do
# Some code which loops through a list
end
end
xml # => <outertag><list></list></outertag>
这篇关于没有精美印刷的Rails XML构建器(即缩小的XML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!