【问题标题】:REXML Formatting issuesREXML 格式问题
【发布时间】:2009-05-07 10:42:50
【问题描述】:

我正在使用 REXML 编辑 xml 文件,但在格式化时遇到了困难。

我的原始代码如下所示:

  file = File.new( destination)
  doc = REXML::Document.new file                         

  doc.elements.each("configuration/continuity2") do |element| 
    element.attributes["islive"]  =  "true"
    element.attributes["pagetitle"]  =  "#{@client.page_title}"
    element.attributes["clientname"]  =  "#{@client.name}"
  end

  doc.elements.each("configuration/continuity2/plans") do |element| 
    element.attributes["storebasedir"]  =  "#{@client.store_dir}"
  end

我首先必须添加以下代码,因为 REXML 是添加单引号而不是双引号。我通过谷歌找到了以下内容:

  REXML::Attribute.class_eval( %q^
    def to_string
      %Q[#@expanded_name="#{to_s().gsub(/"/, '"')}"]
    end
  ^ ) 

我还有一个问题是 REXML 正在重新格式化文档。
有没有办法阻止这种情况?

【问题讨论】:

    标签: ruby rexml


    【解决方案1】:

    关于引号:3.1.7.3 版允许您在元素上使用上下文 cattr_accessor。更新日志:

    http://www.germane-software.com/software/rexml/release.html(动态页面)

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-11
        • 2011-08-16
        • 2011-02-12
        • 2011-11-25
        • 2016-11-29
        • 2019-06-07
        相关资源
        最近更新 更多