REXML 格式问题
我正在使用 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 正在重新格式化文档的问题。
有办法阻止这种情况吗?
I am using REXML to edit an xml file but have ran into difficulties with formatting.
My original code looked like this:
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
I first of all had to add the following code as REXML was adding single quotes instead of double quotes. I found the following via google:
REXML::Attribute.class_eval( %q^
def to_string
%Q[#@expanded_name="#{to_s().gsub(/"/, '"')}"]
end
^ )
I also have a problem in that REXML is reformatting the document.
Are there ways to stop this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关于引号:版本 3.1.7.3 允许您在元素上使用上下文 cattr_accessor。 更改日志:
http://www.germane-software.com/software/rexml/ release.html(动态页面)
About the quotes: version 3.1.7.3 allows you to use the context cattr_accessor on an Element. Changelog:
http://www.germane-software.com/software/rexml/release.html (a dynamic page)
请参阅
Ruby 将 XML 中的单引号转换为双引号
这回答了你的问题
see
Ruby convert single quotes to double quotes in XML
which answers your question