Add Nokogiri::XML::SAX::ParserContext#recovery #453

Merged
merged 2 commits into from Nov 13, 2013

Projects

None yet

3 participants

@libc
Contributor
libc commented May 5, 2011

Hi guys,

This pull request adds an optional ability to recover from structural errors. Sometimes it's useful. I don't change the default value (which is false).

For example,

xml = <<-eoxml
<?xml version="1.0" ?><Root><Data><?xml version='1.0'?><Item>hey</Item></Data><Data><Item>hey yourself</Item></Data></Root>
eoxml

class Doc < Nokogiri::XML::SAX::Document
  def start_element(name, attributes)
    puts "<#{name}>"
  end

  def end_element(name)
    puts "</#{name}>"
  end

  def characters(string)
    puts string
  end
end

parser = Nokogiri::XML::SAX::Parser.new(Doc.new)
parser.parse(xml) do |ctx|
  ctx.recovery = false
end

When recovery is true

<Root>
<Data>
<Item>
hey
</Item>
</Data>
<Data>
<Item>
hey yourself
</Item>
</Data>
</Root>

When recovery is false

<Root>
<Data>
hey
hey yourself

Cheers,
Eugene Pimenov

@libc libc Add SAXParserContext#recovery. If it's set to true, parser will recov…
…er from fatal errors (such as spurious <?xml?> in the middle of the document).
16250b7
kingcu commented Jul 20, 2011

This is great! I had this on my todo list for a few months now. It would be awesome to see this make it upstream.

Member

@flavorjones this looks good to me, any objection to merging this in ?

@jvshahid jvshahid merged commit 4b7a7fb into sparklemotion:master Nov 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment