1

伙计们,

在 Nokogiri 1.5.6 上运行以下代码

require 'nokogiri'

reader = Nokogiri::XML::Reader(<<-eoxml)
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<node label="Session">              
  <node id="session_query1" label="DTM Buffer Size" />
  <node id="session_query2" label="Buffer Block Size"/>
  <node id="session_query3" label="Enable Test Load"/>
</node>
<node label="Workflow" >
 <node id="workflow_query1" label="Enable HA Recovery"/>
 <node id="workflow_query2" label="Suspend on Error"/>
</node>
<node label="Mapping" >
 <node id="mapping_query1" label="SQL Override in SQ " />
 <node id="mapping_query2" label="SQL Override in lookup" />               
</node>
</root>
eoxml

p reader.instance_of? Nokogiri::XML::Reader #true
p reader.xml_encoding #nil

有谁知道为什么 xml_encoding 返回“nil”?

4

1 回答 1

0

查看代码,这是nil因为您通过了它nil

def Reader string_or_io, url = nil, encoding = nil, options = ParseOptions::STRICT
  # ...
end

我猜这是因为它Reader是一个基于事件的解析器,它并不总是有可用的信息,所以它需要传入它。

基于-Document的解析有它:

require 'nokogiri'

p Nokogiri::VERSION

doc = Nokogiri::XML(<<-eoxml)
<?xml version="1.0" encoding="ISO-8859-1"?>
<root>
<node label="Session">              
  <node id="session_query1" label="DTM Buffer Size" />
  <node id="session_query2" label="Buffer Block Size"/>
  <node id="session_query3" label="Enable Test Load"/>
</node>
<node label="Workflow" >
 <node id="workflow_query1" label="Enable HA Recovery"/>
 <node id="workflow_query2" label="Suspend on Error"/>
</node>
<node label="Mapping" >
 <node id="mapping_query1" label="SQL Override in SQ " />
 <node id="mapping_query2" label="SQL Override in lookup" />               
</node>
</root>
eoxml

p doc.encoding
于 2013-10-20T16:44:13.807 回答