469,271 Members | 1,466 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

Unicode and SAXParser

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
Jul 20 '05 #1
3 16456
In article <bq********@netnews.proxy.lucent.com>,
Bobo <bo**@nowhere.com> wrote:
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
--
Spam filter: to mail me from a .com/.net site, put my surname in the headers.

FreeBSD rules!
Jul 20 '05 #2
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

"Richard Tobin" <ri*****@cogsci.ed.ac.uk> wrote in message
news:bq***********@pc-news.cogsci.ed.ac.uk...
In article <bq********@netnews.proxy.lucent.com>,
Bobo <bo**@nowhere.com> wrote:
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
--
Spam filter: to mail me from a .com/.net site, put my surname in the headers.
FreeBSD rules!

Jul 20 '05 #3
In article <bq********@netnews.proxy.lucent.com>,
Bobo <bo**@nowhere.com> wrote:
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. &#xE000;).

-- Richard

--
Spam filter: to mail me from a .com/.net site, put my surname in the headers.

FreeBSD rules!
Jul 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Maciek Zywno | last post: by
8 posts views Thread by Bill Eldridge | last post: by
reply views Thread by hitectahir | last post: by
4 posts views Thread by webdev | last post: by
2 posts views Thread by Neil Schemenauer | last post: by
24 posts views Thread by ChaosKCW | last post: by
1 post views Thread by manish23 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.