# Recovering in Ruby-libxml parser from invalid UTF8 code

**URL:** <https://www.ruby-forum.com/t/recovering-in-ruby-libxml-parser-from-invalid-utf8-code/164614>\
**Category:** Rails\
**Created:** [April 3, 2009, 3:47am UTC](https://www.ruby-forum.com/t/recovering-in-ruby-libxml-parser-from-invalid-utf8-code/164614 "2009-04-03T03:47:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [April 3, 2009, 3:47am UTC](https://www.ruby-forum.com/t/recovering-in-ruby-libxml-parser-from-invalid-utf8-code/164614/1 "2009-04-03T03:47:34Z")

</div>

I am parsing XML streams with ruby-libxml using the XML::Reader class.  
Several have invalid UTF-8 characters. I need a tutorial or at least  
some  
hints on how to recover and continue the parsing.

TIA,  
Jeffrey

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [April 3, 2009, 4:14am UTC](https://www.ruby-forum.com/t/recovering-in-ruby-libxml-parser-from-invalid-utf8-code/164614/2 "2009-04-03T04:14:29Z")

</div>

Jeffrey L. Taylor wrote:

> I am parsing XML streams with ruby-libxml using the XML::Reader class.  
> Several have invalid UTF-8 characters. I need a tutorial or at least some  
> hints on how to recover and continue the parsing.

Why not scrub them with Ruby’s built-in iconv first?

And what are they doing to you and ruby-libxml? I have found libxml2  
suspiciously forgiving, so far…

---

<div class="post-metadata">

**Author:** ![Jeffrey\_LSTaylor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ee7513/32.png) [@Jeffrey\_LSTaylor](https://www.ruby-forum.com/u/Jeffrey_LSTaylor)\
**Post date:** [April 3, 2009, 4:26am UTC](https://www.ruby-forum.com/t/recovering-in-ruby-libxml-parser-from-invalid-utf8-code/164614/3 "2009-04-03T04:26:11Z")

</div>

Quoting P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 

Throws an exception. It took a bunch of digging to find line: 835,  
character:  
418 is truely not an UTF-8 character (octal 240, maybe a Latin-1  
character?).  
I’d like to delete or replace it with a question mark and continue  
parsing.  
It is a rather large file so I’d rather not read the whole thing into  
memory  
to correct. I suppose I could wrap the read function in a clean up  
function.  
Messy trying to keep state for UTF-8 across partial reads.

I was hoping for something better.

Jeffrey
