Created
June 3, 2011 13:39
-
-
Save davidbody/1006357 to your computer and use it in GitHub Desktop.
Parsing XML with Nokogiri
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
#!/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 |
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
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