I come from C background but working on some stuff related to XML in Ruby, so, please bear with me if my question is naive.
I have one XML document. I am parsing it using libxml:
<test>
<ready>
<ex_success>true</ex_success>
</ready>
<ath>
<name>abc</name>
<pass>123</pass>
<ex_success>true</ex_success>
</ath>
</test>
In this document, I am able to read the ex_success
element. However, I am not able to delete it from my original file.
Here is my little piece of code:
require 'xml'
test_file = @file_name
parser = XML::Parser.file(test_file)
document = parser.parse
document.root.each_element {|element|
# Write each element name in the file
puts 'element.name'
if val = element.find_first('ex_success')
puts val.content # prints true
val.remove! # THIS line does not remove the element from my original file
else
puts 'Not found'
end
What am I doing wrong and what is the right way to delete it?