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 = %{
G
}
valid_xml?(bad_xml) # => nil
good_xml = %{
W
Q
}
doc = valid_xml?(good_xml)