# Update existing doc with REXML

**URL:** <https://www.ruby-forum.com/t/update-existing-doc-with-rexml/109104>\
**Category:** Ruby\
**Created:** [July 11, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/update-existing-doc-with-rexml/109104 "2007-07-11T15:32:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![peri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/67e7ee/32.png) [@peri](https://www.ruby-forum.com/u/peri)\
**Post date:** [July 11, 2007, 3:32pm UTC](https://www.ruby-forum.com/t/update-existing-doc-with-rexml/109104/1 "2007-07-11T15:32:49Z")

</div>

I have been searching around the forums and the documentation, but I  
do not see any examples/topics on how to open an existing XML document  
and update a value (element’s text value, for example) using REXML.  
Is it even possible? All I have for a workaround is overwriting the  
existing file using document.write. Am I missing something obvious  
here?

Below is an example I am trying to work with:  
#===========================================  
doc = Document.new File.read(‘cars.xml’)

doc.root.each\_element("//vehicle[make=‘Ford’]") do |file\_node|  
file\_node.elements[‘method’].text = “U”  
end

File.open(‘cars.xml’,‘w’) do |f|  
f.write doc.write  
end

---

<div class="post-metadata">

**Author:** ![peri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/67e7ee/32.png) [@peri](https://www.ruby-forum.com/u/peri)\
**Post date:** [July 11, 2007, 3:38pm UTC](https://www.ruby-forum.com/t/update-existing-doc-with-rexml/109104/2 "2007-07-11T15:38:50Z")

</div>

Hmm, Document#write takes the output stream as an argument… so, I  
think you want:

doc.write(f)

Hope that helps,

Ben
