REXML包裹长行。如何关闭它?

时间:2010-11-17 09:45:42

标签: ruby xml rexml linewrap

我正在使用REXML

创建XML文档
File.open(xmlFilename,'w') do |xmlFile|
    xmlDoc = Document.new
    # add stuff to the document...
    xmlDoc.write(xmlFile,4)
end

一些元素包含相当多的参数,因此相应的行可能会很长。如果它们的长度超过166个字符,则REXML会插入换行符。这当然仍然是完全有效的XML,但我的工作流程包括一些差异和合并,如果每个元素都包含在一行中,则效果最佳。

那么,有没有办法让REXML 插入这些换行换行符?

编辑:我最终通过tidy推送完成的XML文件,作为我脚本的最后一步。如果有人知道更好的方法,我仍然会感激不尽。

2 个答案:

答案 0 :(得分:3)

正如Ryan Calhoun在之前的回答中所说,REXML使用80作为其包装线长度。我很确定这是一个错误(虽然我刚才找不到错误报告)。我能够通过覆盖Formatters :: Pretty类的write_text方法来修复它,以便它使用可配置的@width属性而不是硬编码的80.

require "rubygems"
require "rexml/document"
include REXML

long_xml = "<root><tag>As Ryan Calhoun said in his previous answer, REXML uses 80 as its wrap line length.  I'm pretty sure this is a bug (although I couldn't find a bug report just now).  I was able to *fix* it by overwriting the Formatters::Pretty class's write_text method.</tag></root>"

xml = Document.new(long_xml)

#fix bug in REXML::Formatters::Pretty
class MyPrecious < REXML::Formatters::Pretty
    def write_text( node, output )
        s = node.to_s()
        s.gsub!(/\s/,' ')
        s.squeeze!(" ")

        #The Pretty formatter code mistakenly used 80 instead of the @width variable
        #s = wrap(s, 80-@level)
        s = wrap(s, @width-@level)

        s = indent_text(s, @level, " ", true)
        output << (' '*@level + s)
    end
end

printer = MyPrecious.new(5)
printer.width = 1000
printer.compact = true
printer.write(xml, STDOUT)

答案 1 :(得分:1)

简短回答:是和否。

REXML根据您为indent指定的值使用不同的格式化程序。如果保留默认值-1,则使用REXML::Formatters::Default。如果您给它一个类似4的值,它会使用REXML::Formatters::Pretty。在处理文本(不是标签或属性)时,漂亮的格式化程序确实有逻辑来包装行(尽管看起来它包装在80而不是166)。例如,

的内容
<p> a paragraph tag </p>

将包含80个字符,但

<a-tag with='a' long='list' of='attributes'/>

不会被包裹。

无论如何,80在rexml/formatters/pretty.rb中是硬编码的,不可配置。如果你使用没有缩进的默认格式化程序,那么它主要只是一个没有添加换行符的原始转储。您可以尝试传递格式化程序(请参阅Document.write的文档),但在某些版本的ruby中它已被破坏,可能需要代码破解。无论如何,这可能不是你想要的。


您可以尝试从构建器gem中查看Builder::XmlMarkup

相关问题