使用 REXML 保留 DOCTYPE 声明

发布于 2024-12-13 05:31:45 字数 386 浏览 1 评论 0原文

我正在尝试解析 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戒ㄋ 2024-12-20 05:31:45

恐怕使用 rexml 使用是不可能的。看看这个简短的摘要 - 这是在 rexml 库中发生的过程的“精简版本”,

require 'rexml/source'

LETTER = '[:alpha:]'
COMBININGCHAR = ''
EXTENDER = ''
NCNAME_STR= "[#{LETTER}_:][-[:alnum:]._:#{COMBININGCHAR}#{EXTENDER}]*"

IDENTITY = /^([!\*\w\-]+)(\s+#{NCNAME_STR})?(\s+["'](.*?)['"])?(\s+['"](.*?)["'])?/u
DOCTYPE_PATTERN = /\s*<!DOCTYPE\s+(.*?)(\[|>)/um

string = <<HERE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
</log4j:configuration>
HERE
source = REXML::SourceFactory.create_from(string)
md = source.match( DOCTYPE_PATTERN, true )
identity = md[1]
close = md[2]
identity =~ IDENTITY
name = $1
pub_sys = $2.nil? ? nil : $2.strip
long_name = $4.nil? ? nil : $4.strip
uri = $6.nil? ? nil : $6.strip
args = [ :start_doctype, name, pub_sys, long_name, uri ]
p args  # => [:start_doctype, "log4j", nil, nil, nil]

正如您所看到的,此代码片段返回与问题中的代码相同的结果。除此之外,您会看到代码片段中没有可以更改此行为的参数。

作为解决方法,我建议您使用 Nokogiri 库。快速浏览一下它可以正确解析此类文档类型:

require 'nokogiri'

string = <<HERE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
</log4j:configuration>
HERE

doc = Nokogiri::XML(string)
puts doc.internal_subset.to_s
# => <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

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 in rexml library

require 'rexml/source'

LETTER = '[:alpha:]'
COMBININGCHAR = ''
EXTENDER = ''
NCNAME_STR= "[#{LETTER}_:][-[:alnum:]._:#{COMBININGCHAR}#{EXTENDER}]*"

IDENTITY = /^([!\*\w\-]+)(\s+#{NCNAME_STR})?(\s+["'](.*?)['"])?(\s+['"](.*?)["'])?/u
DOCTYPE_PATTERN = /\s*<!DOCTYPE\s+(.*?)(\[|>)/um

string = <<HERE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
</log4j:configuration>
HERE
source = REXML::SourceFactory.create_from(string)
md = source.match( DOCTYPE_PATTERN, true )
identity = md[1]
close = md[2]
identity =~ IDENTITY
name = $1
pub_sys = $2.nil? ? nil : $2.strip
long_name = $4.nil? ? nil : $4.strip
uri = $6.nil? ? nil : $6.strip
args = [ :start_doctype, name, pub_sys, long_name, uri ]
p args  # => [:start_doctype, "log4j", nil, nil, nil]

As 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:

require 'nokogiri'

string = <<HERE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >
<log4j:configuration>
</log4j:configuration>
HERE

doc = Nokogiri::XML(string)
puts doc.internal_subset.to_s
# => <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文