Skip to content

Instantly share code, notes, and snippets.

@znz
Last active December 25, 2015 17:59
Show Gist options
  • Save znz/7017677 to your computer and use it in GitHub Desktop.
Save znz/7017677 to your computer and use it in GitHub Desktop.
Nokogiri::XML::Reader and attribute
% ruby tameshi-giri.rb
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin11.4.2]
Nokogiri::VERSION = 1.6.0
Nokogiri::LIBXML_VERSION = 2.8.0
"value"
"http://example.com/"
% ruby tameshi-giri.rb
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]
Nokogiri::VERSION = 1.6.0
Nokogiri::LIBXML_VERSION = 2.8.0
"value"
"http://example.com/"
#!/usr/bin/env ruby
require 'nokogiri'
puts RUBY_DESCRIPTION
puts "Nokogiri::VERSION = #{Nokogiri::VERSION}"
puts "Nokogiri::LIBXML_VERSION = #{Nokogiri::LIBXML_VERSION}"
xml = <<-XML
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://example.com/">
<foo name="value"/>
<bar/>
</root>
XML
reader = Nokogiri::XML::Reader(xml) do |config|
config.strict.nonet
end
reader.each do |node|
next unless /foo|bar/ =~ node.name
p node.attribute("name")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment