# Rexml exceptions

**URL:** <https://www.ruby-forum.com/t/rexml-exceptions/149811>\
**Category:** Ruby\
**Created:** [September 30, 2008, 4:58am UTC](https://www.ruby-forum.com/t/rexml-exceptions/149811 "2008-09-30T04:58:22Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [September 30, 2008, 4:58am UTC](https://www.ruby-forum.com/t/rexml-exceptions/149811/1 "2008-09-30T04:58:22Z")

</div>

Is there any way to get any useful data out of REXML::ParseException  
when you’re working on a String? It never sets @position or @line or  
anything. I need to figure out exactly where the error-causing tag  
starts, and save it.

Any ideas?

– rakaur

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [September 30, 2008, 4:01pm UTC](https://www.ruby-forum.com/t/rexml-exceptions/149811/2 "2008-09-30T16:01:47Z")

</div>

On Sep 29, 10:56 pm, Eric W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is there any way to get any useful data out of REXML::ParseException  
> when you’re working on a String? It never sets @position or @line or  
> anything. I need to figure out exactly where the error-causing tag  
> starts, and save it.
> 
> Any ideas?

Out of curiosity, I tried libxml. It has nice error messages:

foo.xml:3:  
parser error :  
Opening and ending tag mismatch: title line 3 and txitle  
Foo  
^

but they go to stdout. You can capture them by registering an error  
handler. Sample code:

parser = XML::Parser.new  
parser.filename = “foo.xml”

msgs = []  
XML::Parser.register\_error\_handler lambda { |msg| msgs \<\< msg }

begin  
parser.parse  
rescue Exception =\> e  
puts “Error: #{msgs}”  
end

– Mark.

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [September 30, 2008, 6:19pm UTC](https://www.ruby-forum.com/t/rexml-exceptions/149811/3 "2008-09-30T18:19:29Z")

</div>

On Tue, Sep 30, 2008 at 9:59 AM, Mark T. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> end  
> Interesting. I was thinking about doing libxml anyway. I do not like  
> REXML.

Thanks.

> – Mark.

– rakaur

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [September 30, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/rexml-exceptions/149811/4 "2008-09-30T19:41:47Z")

</div>

On Sep 30, 12:46 pm, Eric W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Actually, this isn’t working for me. I’m using the SAX parser, and it  
> just calls Listener#on\_parser\_error with a string. Not helping me.

Why would you want to do that? You already have the XML as a string.  
The only reason to put up with the awful interface and extra  
complexity of SAX would be if your file doesn’t fit into memory. And I  
don’t think the SAX interface to libxml is as complete/robust yet as  
the DOM interface.

Go with the DOM interface. With libxml it’s plenty fast.

– Mark.

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [September 30, 2008, 8:10pm UTC](https://www.ruby-forum.com/t/rexml-exceptions/149811/5 "2008-09-30T20:10:31Z")

</div>

My situation requires SAX, unfortunately.

I need to parse and react to each tag as in comes in. If there’s a  
broken one, all tags up to the broken one must be processed, and the  
broken one must be stored. I cannot do this in DOM, because if there’s  
an error, DOM will not process anything.

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [September 30, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/rexml-exceptions/149811/6 "2008-09-30T18:48:18Z")

</div>

Actually, this isn’t working for me. I’m using the SAX parser, and it  
just calls Listener#on\_parser\_error with a string. Not helping me.

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [September 30, 2008, 8:53pm UTC](https://www.ruby-forum.com/t/rexml-exceptions/149811/7 "2008-09-30T20:53:29Z")

</div>

Also, I don’t think those error messages can help me location the  
position in the string of the bad XML. They’re pretty, for sure, but  
not very useful to anyone but a human.

---

<div class="post-metadata">

**Author:** ![ericw](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/97f17d/32.png) [@ericw](https://www.ruby-forum.com/u/ericw)\
**Post date:** [September 30, 2008, 8:55pm UTC](https://www.ruby-forum.com/t/rexml-exceptions/149811/8 "2008-09-30T20:55:55Z")

</div>

On Sep 30, 2:09 pm, Eric W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> My situation requires SAX, unfortunately.
> 
> I need to parse and react to each tag as in comes in. If there’s a  
> broken one, all tags up to the broken one must be processed, and the  
> broken one must be stored. I cannot do this in DOM, because if there’s  
> an error, DOM will not process anything.

If you can receive an entire document at a time, libxml has a  
‘recover’ mode that will correct what it can and process the entire  
document – even if it is not well-formed. It works surprisingly well.

Another option is writing your own recursive descent parser. See  
[http://snippets.dzone.com/posts/show/2190](http://snippets.dzone.com/posts/show/2190) for a starting point.

– Mark.
