I've got an XML file that has a ton of comments that make the file super large and muddy. Is it possible to delete the comments out of it with REXML?
I've tried this, but it isn't working (though, strangely enough, its not failing either):
doc.elements.each('//comment()') { |n| doc.delete n }
UPDATE
This works:
require 'rexml/document'
doc = REXML::Document.new "<root><foo><!-- comment --></foo></root>"
doc.elements('//*').each { |n| n.comments().each { |c| c.parent = nil } }
formatter = REXML::Formatters::Pretty.new(4)
formatter.compact = true
puts formatter.write(doc.root, '')
# Output:
#
# <root>
# <foo/>
# </root>
I got the solution from here (ruby-doc.org).
Try
A complete snippet is
which outputs
so all comments are removed.