2

我正在使用 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 正在重新格式化文档。
有没有办法阻止这种情况?

4

2 回答 2

0

Ruby 将 XML 中的单引号转换为双引号

这回答了你的问题

于 2011-04-07T23:46:17.310 回答
0

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

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

于 2009-08-14T10:26:38.697 回答