使用'Reader'和Nokogiri解析XML文档会返回LibXML错误

时间:2009-11-12 04:43:44

标签: xml nokogiri html-parsing

在使用Nokogiri阅读器解析XML文件时,我是新手。这是我要解析的XML文件和示例代码:

<?xml version='1.0' encoding='UTF-8'?>
<inventory>
  <tire name="super slick racing tire" />
  <tire name="all weather tire" />
</inventory>
-----------------------------------------------------------------
require 'rubygems'
require 'nokogiri'

io = File.open('test.xml', 'r')
reader = Nokogiri::XML::Reader(io)

reader.each do |node|

# node is an instance of Nokogiri::XML::Readerruby
puts node.name

end

以下是我收到的错误消息:

pcs$ ruby1.9 TestNok.rb
WARNING: Nokogiri was built against LibXML version 2.6.32, but has dynamically loaded 2.7.5
/usr/lib/ruby/1.9.0/nokogiri/xml/reader.rb:60:in `read': ParsePI: PI xm never end ...  (Nokogiri::XML::SyntaxError)
from /usr/lib/ruby/1.9.0/nokogiri/xml/reader.rb:60:in `each'
from TestNok.rb:7:in `<main>'
<dummy toplevel>: [BUG] Segmentation fault
ruby 1.9.0 (2008-10-04 revision 19669) [i486-linux]

-- control frame ----------
c:0001 p:0000 s:0002 b:0002 l:000001 d:000001 TOP    
---------------------------
-- backtrace of native function call (Use addr2line) --
0xb08316
0xa285e7
0xa2866a
0xab1144
0x9a0410
0xa5f315
0xa2b994
0xa2baae
0x80487e8
0x469b56
0x80486e1
-------------------------------------------------------
Aborted

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

您粘贴的代码在我的计算机上运行:

jablan@jablan-hp:~/dev$ ruby testxml.rb 
inventory
#text
tire
#text
tire
#text
inventory
jablan@jablan-hp:~/dev$ ruby -v
ruby 1.9.1p243 (2009-07-16 revision 24175) [i686-linux]
jablan@jablan-hp:~/dev$ gem list | grep nokogiri
nokogiri (1.4.0)

答案 1 :(得分:0)

似乎是读者解析器的重复数据。

inventory
tire
tire
inventory

必须是:

inventory
tire