Created
April 4, 2014 23:07
-
-
Save carlzulauf/9984818 to your computer and use it in GitHub Desktop.
Issue with overcoming errors when using nokogiri's SAX parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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