民间,
在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”?
答案 0 :(得分: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