0

I have an empty XML page I've called "users.xml". I'd like to be able to create all the content using REXML.

require "rexml/document"
include REXML  # so that we don't have to prefix everything with REXML::...

xmlfile = File.new("users.xml")
doc = Document.new(xmlfile)

//code to save root element here...

It looks to me that doc reads first the content of "users.xml", but changes that occurred in doc do not propagate back. How do I save changes to the file?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Richard77
  • 20,343
  • 46
  • 150
  • 252
  • Why do you want to use REXML? [Nokogiri](http://nokogiri.org) is pretty much the default standard for processing XML in Ruby. – the Tin Man Oct 30 '12 at 06:08
  • @theTinMan, because ruby is new to me I do normally .net. I'll try Nokogiri then. – Richard77 Oct 30 '12 at 06:14
  • 1
    Read through, and try the tutorials for Nokogiri. Search the SO archives for the `[nokogiri]` tag if you have questions. There are a good number of excellent examples of how to do things here. – the Tin Man Oct 30 '12 at 06:17

1 Answers1

1

You open the file:

doc = Document.new(xmlfile)

but you never read it, or write it again.

You need to use something like:

xmlfile = File.read("users.xml")

to read it, (which isn't a scalable way to do it, but that's an entirely different subject).

After you convert it to a REXML document using doc = Document.new(xmlfile), you need to write it back out. You can use File.write or File.open with a block.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303