Your problem is that you are looking for an element named old_A when you should look for an element with the textual content of old_A .
Here's a solution using Nokogiri, which I find more convenient than REXML:
require 'nokogiri' # gem install nokogiri xml = "<Standart-Profile> <class1> <class2> <class3> <value1>old_A</value2> <value1>old_B</value2> <value1>old_C</value2> </class3> </class2> </class1> </Standart-Profile>" doc = Nokogiri.XML(xml) doc.at('//text()[.="old_A"]').content = 'generate_1' doc.at('//text()[.="old_B"]').content = 'generate_2' doc.at('//text()[.="old_C"]').content = 'generate_3' File.open('output.xml','w') do |f| f.puts doc end #=> <?xml version="1.0" encoding="US-ASCII"?> #=> <Standart-Profile> #=> <class1> #=> <class2> #=> <class3> #=> <value1>generate_1</value1> #=> <value1>generate_2</value1> #=> <value1>generate_3</value1> #=> </class3> #=> </class2> #=> </class1> #=> </Standart-Profile>
If you really want to call the generate_1 method (as you defined), you should use instead:
...content = generate_1 # no quotes
Change Here is one way to do this using XPath in REXML (after fixing the XML source code):
require 'rexml/document' doc = REXML::Document.new(xml) REXML::XPath.first(doc,'//*[text()="old_A"]').text = 'generate_1' REXML::XPath.first(doc,'//*[text()="old_B"]').text = 'generate_2' REXML::XPath.first(doc,'//*[text()="old_C"]').text = 'generate_3' puts doc #=> <Standart-Profile> #=> <class1> #=> <class2> #=> <class3> #=> <value1>generate_1</value1> #=> <value1>generate_2</value1> #=> <value1>generate_3</value1> #=> </class3> #=> </class2> #=> </class1> #=> </Standart-Profile>
source share