Skip to content

Instantly share code, notes, and snippets.

@carlzulauf
Created April 4, 2014 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlzulauf/9984818 to your computer and use it in GitHub Desktop.
Save carlzulauf/9984818 to your computer and use it in GitHub Desktop.
Issue with overcoming errors when using nokogiri's SAX parser
require 'nokogiri'
class Doc < Nokogiri::XML::SAX::Document
include Enumerable
def initialize(xml)
@xml = xml
end
def each(&block)
@on_record = block
parse(@xml)
end
def parse(xml)
parser = Nokogiri::XML::SAX::Parser.new(self)
parser.parse(xml)
end
def end_element(name)
@on_record.call(name) if name == "details"
end
def error(str)
puts str
end
end
xml = <<XML
<?xml version="1.0" encoding="UTF-8"?>
<streeteasy version="1.5">
<properties>
<property url="http://example.com/?foo=bar&yin=yang">
<location>Somewhere</location>
<details>Information goes here</details>
</property>
</properties>
</streeteasy>
XML
puts Doc.new(xml).count # => 0, but should be 1
puts Nokogiri::XML(xml).xpath("//details").count # => 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment