Checking XML Well-Formedness
require 'rexml/document' def valid_xml?(xml) begin REXML::Document.new(xml) rescue REXML::ParseException # Return nil if an exception is thrown end end bad_xml = %{ <tasks> <pending> <entry>G</entry> <done> <entry>D</entry> </tasks>} valid_xml?(bad_xml) # => nil good_xml = %{ <groceries> <bread>W</bread> <bread>Q</bread> </groceries>} doc = valid_xml?(good_xml)