我正在使用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正在重新格式化文档。
有什么方法可以阻止这一切吗?
发布于 2009-08-14 10:26:39
关于报价:版本3.1.7.3允许您在元素上使用上下文cattr_accessor。Changelog:
http://www.germane-software.com/software/rexml/release.html (动态页面)
发布于 2011-04-08 07:46:17
https://stackoverflow.com/questions/833942
复制相似问题