How to modify a text value in a xml?

M

Milo Luo

Hi, folks
I met a problem with xml. I can't write to the file directly, here is my
code:

f = File.open(FILE_NAME)
xml = REXML::Document.new(f)

xml.root.each_element do |xml|
xml.each_element do |node|
#if the node name equal to Properties
if node.name == "Properties"
node.each_element do |child|
#Get the child of Properties
if child.name == "Name"
#If the text equal to English, modified to the text to ABC
if child.get_text == "English"
#What should I put here?
child.text = "ABC"
xml.write(f)
end
end
end

end
end
end

f.close

I want all the contents the same as the original file except the text of
the node "Name".
How can I do that?

Thanks very much.

BR,
Milo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top