Unicode and SAXParser

B

Bobo

I am getting the following error while trying to process an XML string with
unicode in its content.

[Fatal Error] :12:0: An invalid XML character (Unicode: 0x0) was found in
the eement content of the document.
SAXException: org.xml.sax.SAXParseException: An invalid XML character
(Unicode:0x0) was found in the element content of the document.

The piece of code that generates this error is the following:

DOMParser parser = new DOMParser();
ByteArrayInputStream bais = new
ByteArrayInputStream(xmlString.getBytes());
InputSource input = new InputSource(bais) ;
XMLDocWriter docwriter = new XMLDocWriter();
parser.parse(input); // <== right here is where I get this error at
run time

Could you please give me a piece of advise what to do? I am pretty new to
XML. I am using apache xerces java API.

Any help or hint would be appreciated!

Yung
 
R

Richard Tobin

Bobo said:
SAXException: org.xml.sax.SAXParseException: An invalid XML character
(Unicode:0x0) was found in the element content of the document.

Well somehow you've got a null in the input stream, which is not
allowed. Assuming that xmlString didn't contain a null:
ByteArrayInputStream bais = new ByteArrayInputStream(xmlString.getBytes());

the obvious way for this could happen would be for getBytes() to be
encoding the string in a different encoding from that used to parse
the resulting byte stream. That's quite likely - the parser is
presumably using UTF-8 by default and getBytes uses the platform's
default encoding which is probably not UTF-8, but I can't see how
you're likely to get a null with the common default encodings. Is
there an XML declaration at the start of the string, and if so is it
specifying the encoding?

-- Richard
 
B

Bobo

Thanks for the reply. No. This happens be an error message from a telnet
session which has a lot of escape characters. Right now I am thinking about
ignoring them and escape them. If there's a way to pass all those
characters, it would be great to know. Do you have any thoughts on this?

Thanks,
Yung
 
R

Richard Tobin

Bobo said:
Thanks for the reply. No. This happens be an error message from a telnet
session which has a lot of escape characters. Right now I am thinking about
ignoring them and escape them. If there's a way to pass all those
characters, it would be great to know. Do you have any thoughts on this?

You certainly need to do something about them!

Most control characters are illegal in XML 1.0. 1.1 is more liberal,
but you probably don't want to rely on 1.1 just for this, and it won't
help with nuls. If you want to preserve the characters you could:

(a) translate them into elements, e.g. <control code="0"/>
(b) translate them into other characters, perhaps ones in the
unicode private use area (you would probably translate them
into character references, e.g. ).

-- Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top