Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] Dealing with incorrect unicode

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: "Marcus Downing" <marcus AT minotaur.it>
  • To: xom-interest AT lists.ibiblio.org
  • Subject: [XOM-interest] Dealing with incorrect unicode
  • Date: Sat, 3 Nov 2007 10:48:46 +0000

XOM is throwing an exception when I tell it to parse in UTF-8 a stream
that contains stray bytes, namely:

<?xml version="1.0"?>
...<line>113 Heath Ã…venue</line>...

Note the circle over the A. Pasting it into thie email obviously means
you aren't seeing the same bytes as the program, so I've attached the
file. I believe that the data is written in ISO-8859-1, while XOM is
trying to read it as UTF-8. So the exception is correct.

My code is simply:

ByteArrayInputStream stream = new ByteArrayInputStream(data);
Builder builder = new Builder();
builder.build(stream);

And the exception it reports is:

nu.xom.ParsingException: Invalid byte 2 of 2-byte UTF-8 sequence. at
line 2, column 43
at nu.xom.Builder.build(Builder.java:1132)
at nu.xom.Builder.build(Builder.java:586)
... 23 more
Caused by: org.xml.sax.SAXParseException: Invalid byte 2 of 2-byte
UTF-8 sequence.
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1269)
at nu.xom.Builder.build(Builder.java:1127)

This behaviour is correct - I really am feeding it garbage, and it's
right to complain. The data comes from an old broken system I'm
importing from. There are several such instances throughout the data,
and correcting them manually would be a bore.

So I'd quite like to do something other than die when my code
encounters this. I'd prefer to try alternative ways of parsing the
data. Unfortunately, when I attempt to parse the same bytes through a
Reader with "ISO-8859-1" as its character set, I get this exception:

[Fatal Error] :-1:-1: Premature end of file.
nu.xom.ParsingException: Premature end of file.
at nu.xom.Builder.build(Builder.java:1132)
at nu.xom.Builder.build(Builder.java:1019)
...
Caused by: org.xml.sax.SAXParseException: Premature end of file.
at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1269)
at nu.xom.Builder.build(Builder.java:1127)

There are suggestions on the net - backed up by my own tentative
digging with a debugger - that this is actually Xerces complaining
that it wasn't able to download a schema.

Does anybody have an idea how I can force it to read the data, even at
the expense of losing the odd character? Or how I can get rid of the
second exception?

--
Marcus Downing
marcus AT minotaur.it



Archive powered by MHonArc 2.6.24.

Top of Page