# Overwrite a File (rexml)

**URL:** <https://www.ruby-forum.com/t/overwrite-a-file-rexml/139864>\
**Category:** Ruby\
**Created:** [June 8, 2008, 11:49am UTC](https://www.ruby-forum.com/t/overwrite-a-file-rexml/139864 "2008-06-08T11:49:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [June 8, 2008, 11:49am UTC](https://www.ruby-forum.com/t/overwrite-a-file-rexml/139864/1 "2008-06-08T11:49:30Z")

</div>

Hello,

I am trying to get some simple create/update/delete xml-element to  
work with REXML, but I run into trouble when writing to file. Here’s a  
code example:

def save  
file = File.open(@@filePath, “r+”)  
doc = Document.new(file)  
root = doc.root

```
            #create/update/delete elements in the root

      file.rewind
formatter = REXML::Formatters::Default.new( 5 )
formatter.write( doc, file)
file.close

```

end

at first I was just adding and updating, and I noticed I needed to put  
the line:  
file.rewind  
or it would just append the root elements to the existing ones in the  
file, thereby creating doubles.  
Now that I have reached delete, I get the following behaviour:

say the file contains:

element\_1  
element\_2  
element\_3  
element\_4

and I want to delete element\_2, the result in the file is:

element\_1  
element\_3  
element\_4  
element\_4

to me, this:  
formatter.write( doc, file)  
implies that the entire xml doc would be written to the file, and as a  
result the file should only contain the doc (or the root of the doc),  
and nothing else.

I am not very experienced in Ruby, and tried to resolve this way:

def save  
file = File.open(@@filePath, “r+”)  
doc = Document.new(file)  
root = doc.root  
file.close

```
            #create/update/delete elements in the root

file = File.open(@@filePath, "w")
formatter = REXML::Formatters::Default.new( 5 )
formatter.write( doc, file)
file.close

```

end

this works, but I feel there should be a better approach, because  
having to open the same file twice seems awkward .

Thanks in advance,

Rob

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [June 9, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/overwrite-a-file-rexml/139864/2 "2008-06-09T14:53:57Z")

</div>

2008/6/8 Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> file = File.open(@@filePath, “r+”)
> 
> element\_4
> 
> formatter.write( doc, file)  
> file.close
> 
> end
> 
> this works, but I feel there should be a better approach, because  
> having to open the same file twice seems awkward .
> 
> Thanks in advance,

IMHO you need to seek and truncate the file when opened in rw mode.  
Otherwise you do not have control about where the document is written.  
Since you are first reading it it is likely that the new content  
simply gets appended. HTH

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![robtotheb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/db5fbb/32.png) [@robtotheb](https://www.ruby-forum.com/u/robtotheb)\
**Post date:** [June 9, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/overwrite-a-file-rexml/139864/3 "2008-06-09T15:21:41Z")

</div>

Rob B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to get some simple create/update/delete xml-element to  
> work with REXML, but I run into trouble when writing to file. Here’s a  
> code example:

instead of using rexml, I’d suggest you ‘xml/smart’ which as a “modify”  
method.

for example I’m using :  
XML::Smart.modify( “#{file}” ) do | doc |  
node=doc.root.find(“//xhtml:p[@id = ‘maj’]”, {“xhtml” =\>  
“[XHTML namespace](http://www.w3.org/1999/xhtml)”}).first  
if node  
node.text=File.stat(“#{file}”).mtime.strftime(“màj le %d %m %y  
%H:%M”)  
puts “file ‘#{file}’ updated!”  
else  
puts “No node "//xhtml:p[@id = ‘maj’]" for file ‘#{file}’!”  
end  
end

in order to modify the date (writen in

\<

) from file’s  
mtime.
