Skip to content

Instantly share code, notes, and snippets.

@janv
Created November 9, 2010 12:30
Show Gist options
  • Save janv/669029 to your computer and use it in GitHub Desktop.
Save janv/669029 to your computer and use it in GitHub Desktop.
Example for using the nokogiri reader
require 'rubygems'
require 'nokogiri'
doc = Nokogiri::XML.fragment(<<-eoxml)
<bla>
<bing/>
<bong/>
</bla>
<blubb>asdasd</blubb>
eoxml
# puts doc
reader = Nokogiri::XML::Reader(<<-eoxml, nil, nil, Nokogiri::XML::ParseOptions::DEFAULT_XML ^ Nokogiri::XML::ParseOptions::NOBLANKS)
<doc>
<bla>
<bing a="1" b="2"/>
<bong/>
</bla>
<blubb>111111</blubb>
<blubb>222222</blubb>
<blubb>333333</blubb>
<blubb>444444</blubb>
</doc>
eoxml
reader.each do |node|
puts "#{' '*node.depth}Name: #{node.name} Type: #{node.node_type}"
puts node.outer_xml
puts '--------------------------------'
end
# node type 15 = end node
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment