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