当我运行以下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字符串以使其具有单独的结束元素而不是自动关闭的根标记,则异常就会消失。