使用 REXML 保留 DOCTYPE 声明
我正在尝试解析 log4j.xml 文件,编辑一些属性,然后将其写回。
log4j.xml 具有 声明,但是当我写回它时,声明更改为
。
我已使用 xmlDoc = Document.new(File.new(file, 'r'))
打开文件进行解析,并使用 xmlDoc.write(File.new(file, 'w'), 0)
。
我还尝试使用 xmlDoc = Document.new(File.new(file, 'r'), { :raw => :all })
打开。
有没有办法保留原始的 DOCTYPE 声明?
非常感谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕使用
rexml
使用是不可能的。看看这个简短的摘要 - 这是在rexml
库中发生的过程的“精简版本”,正如您所看到的,此代码片段返回与问题中的代码相同的结果。除此之外,您会看到代码片段中没有可以更改此行为的参数。
作为解决方法,我建议您使用
Nokogiri
库。快速浏览一下它可以正确解析此类文档类型: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: