views:

602

answers:

2

Hi. I am a newbie when it comes to using Nokogirie reader to parse an xml file. Here is the xml file I want to parse and sample code:

<?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

The following is the error message I get:

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

Any help would be greatly appreciated.

+1  A: 

The code that you pasted works on my machine:

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)
Mladen Jablanović
A: 

Seems be the reader parser duplicated data. inventory tire tire inventory

It must be:

inventory tire

D.c