2

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?

Nakilon
  • 34,866
  • 14
  • 107
  • 142
dagda1
  • 26,856
  • 59
  • 237
  • 450

2 Answers2

0

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)

0

see

Ruby convert single quotes to double quotes in XML

which answers your question

Community
  • 1
  • 1
Ingo
  • 1,805
  • 18
  • 31