REXML 格式问题

发布于 2024-07-18 17:08:36 字数 826 浏览 10 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放肆 2024-07-25 17:08:36

关于引号:版本 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文