Reputation: 41
I have written a code that remove comments on xml file, how can i save it as an xml file again. Currently i read the xml file as a text file. When i run this code on the console it outputs the xml content, but on the newexample.xml it prints out this : **#<File:0x00000002b8be40>**
How can i print it as an xml file? please help
xml before edited
<!--
<product>
<name>PC</name>
<price>R100</price>
</product>
-->
Output: xml after editing
<product>
<name>PC</name>
<price>R100</price>
</product>
here is the code
file = File.open('C:/ruby/example.xml','r+') do |file|
file.each {|line| line.gsub(/^\s\W*\s$/, " ")}
end
File.open("newexample.xml","w") do |f|
f.write file
end
Upvotes: 0
Views: 86
Reputation: 75488
You can have
#!/usr/bin/env ruby
file_path = 'C:/ruby/example.xml'
File.open(file_path, 'r') do |file|
@a = file.map{ |line| line.gsub(/^\s\W*\s$/, " ") }
end
File.open(file_path, 'w') do |file|
@a.each { |e| file.puts e }
end
Upvotes: 1