I'm trying to parse a log4j.xml file, edit some attributes, and write it back.
The log4j.xml has the <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
declaration, but when I write it back, the declaration changed to <!DOCTYPE log4j>
.
I've opened the file for parsing with xmlDoc = Document.new(File.new(file, 'r'))
, and wrote with xmlDoc.write(File.new(file, 'w'), 0)
.
I've also tried opening with xmlDoc = Document.new(File.new(file, 'r'), { :raw => :all })
.
Is there a way to preserve the original DOCTYPE declaration?
Thank you very much!
I'm afraid that isn't possible with
rexml
usage. Look at that brief summary - this is the 'light version' of the process that take place inrexml
libraryAs you can see this snippet returns the same result as your code in the question. And beside this you see that there are no parameters in the snippet that can change this behavior.
As a workaround I can suggest you to use
Nokogiri
library. At the quick look it can parse such doctype properly: