From: Ross Bamford Date: 2006-05-08T03:56:24+09:00 Subject: Re: REXML exception rescuing On Mon, 2006-05-08 at 03:21 +0900, Eli Bendersky wrote: > Hi all, > > I use the following to parse an XML file with REXML: > > begin > doc = REXML::Document.new File.new('test_cfg.xml') > rescue REXML::ParseException => msg > puts "Failed: #{msg}" > end > > When REXML throws ParseException, a whole load information is spilled > into msg. Although this is useful for debugging, it hardly is an option > for a "real-life" application that just has to notify the user of an > error in his .xml file in the friendliest way possible. > > How can I get just the "malformed XML: missing tag start" part (plus the > line in the file) without all the stack trace and the information that > comes after it, which for some reason is repeated twice. Is this > possible without deconstructing the raised msg ? This is probably a bit naive but maybe will be a start: begin doc = REXML::Document.new File.new('bad.xml') rescue REXML::ParseException => ex puts "Failed: #{ex.message[/^.*$/]} (#{ex.message[/Line:\s\d+/]})" end -- Ross Bamford - rosco@roscopeco.REMOVE.co.uk