Ruby中的XML解析失败,使用standalone =" yes"属性(libxml-ruby 2.3.3 gem)

时间:2014-08-01 12:18:44

标签: ruby-on-rails ruby libxml2

当我运行以下Ruby代码时:

xml_str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><project name=\"ProjectA\"/>"

parser = XML::Reader.string(xml_str, :encoding => XML::Encoding::UTF_8,
                                :options => ( XML::Parser::Options::NOENT | XML::Parser::Options::NOBLANKS))
parser.read

它抛出异常:

Fatal error: Extra content at the end of the document at :1.
LibXML::XML::Error: Fatal error: Extra content at the end of the document at :1.
from (irb):55:in `read'
from (irb):55
from /usr/local/rvm/gems/ruby-2.1.2/gems/railties-3.2.19/lib/rails/commands/console.rb:47:in `start'
from /usr/local/rvm/gems/ruby-2.1.2/gems/railties-3.2.19/lib/rails/commands/console.rb:8:in `start'
from /usr/local/rvm/gems/ruby-2.1.2/gems/railties-3.2.19/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

有人能说出这段代码失败的原因吗?

注意:如果我删除standalone=\"yes\"或更改XML字符串以使其具有单独的结束元素而不是自动关闭的根标记,则异常就会消失。

0 个答案:

没有答案