[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Update existing doc with REXML

peri

7/9/2007 4:59:00 PM

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

1 Answer

bmunat@gmail.com

7/9/2007 7:00:00 PM

0

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

doc.write(f)

Hope that helps,

Ben



On Jul 9, 6:59 am, peri <mtperit...@gmail.com> wrote:
> 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