Skip to content

Instantly share code, notes, and snippets.

@davidbody
Created June 3, 2011 13:39
Show Gist options
  • Save davidbody/1006357 to your computer and use it in GitHub Desktop.
Save davidbody/1006357 to your computer and use it in GitHub Desktop.
Parsing XML with Nokogiri
#!/usr/bin/env ruby
require 'nokogiri'
xml = <<XML_END
<?xml version="1.0"?>
<root><this><document><is>truncated
XML_END
puts "Original:"
puts xml
puts "\nNokogiri:"
puts Nokogiri::XML(xml).to_xml
puts "\nNokogiri strict:"
begin
puts Nokogiri::XML(xml, nil, nil, Nokogiri::XML::ParseOptions::STRICT).to_xml
rescue Nokogiri::XML::SyntaxError => e
puts e
end
Original:
<?xml version="1.0"?>
<root><this><document><is>truncated
Nokogiri:
<?xml version="1.0"?>
<root>
<this>
<document>
<is>truncated
</is>
</document>
</this>
</root>
Nokogiri strict:
Premature end of data in tag root line 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment