You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by Gary Kephart <ga...@emotion.com> on 2000/09/06 23:35:26 UTC
The root element is required... [was Re: An invalid XML character
(Unicode: 0x0)...]
Gary Kephart wrote:
> Eric Ye wrote:
>
> > The cause of your problem is that the file is encoded in "UTF-16" and there
> > is no <?xml version="1.0" encoding="UTF-16" in the beginning file, so the
> > parser will assume this is a UTF-8 encoded file(UTF-8 is the default
> > encoding), that is why it sees byte 0x00 then throws the invalid Unicode
> > exception.
> >
> > Two alternatives to solve this:
> > 1. pre convert the file to UTF-8 encoding before pass it to the parser.
> > 2. change the source to include <?xml version="1.0" encoding="UTF-16"?> at
> > the very beginning.
Ok, I caught my mistake. I was doing this:
this.inputSource = new InputSource(new
DataInputStream(this.socket.getInputStream()));
when I should have been doing just this:
this.inputSource = new InputSource(this.socket.getInputStream());
Overcompensating for Unicode handling, I guess. Now I get this:
org.xml.sax.SAXParseException: The root element is required in a well-formed
document.
Anyone know why? It seems to be unable to read anything from the stream, although
I've checked that the client app is sending XML through the socket stream to my
program.
Thanks in advance,
Gary
>
>
> > Eric Ye * IBM, JTC - Silicon Valley * ericye@locus.apache.org
> >
> > ----- Original Message -----
> > From: "Gary Kephart" <ga...@emotion.com>
> > To: <xe...@xml.apache.org>
> > Sent: Friday, September 01, 2000 4:05 PM
> > Subject: An invalid XML character (Unicode: 0x0)...
> >
> > > ... was found in the prolog of the document.
> > >
> > > This is the error I'm getting. Attached is the XML file. I'm using JDK
> > 1.2.2
> > > and Xerces 1.1.3. There's no DTD file. Below are excerpts from my code.
> > I'm
> > > feeding the XML file into a client program, and it's sending the XML over
> > a
> > > socket. The client program was written in C++ (but not by me).
> > > What, in general, are the reasons this exception is thrown? How do I get
> > > around it?
> > >
> > > Socket clientSocket = this.serverSocket.accept();
> > > this.inputSource = new InputSource(new
> > > DataInputStream(this.socket.getInputStream()));
> > > this.saxParser.parse(this.inputSource);
--
Gary Kephart, Software Engineer | eMotion, Inc.
Phone: (310)914-2771 | 11835 West Olympic, Suite 1000
Fax: (310)479-8480 | Los Angeles, CA 90064
mailto:gary_kephart@acm.org | http://www.emotion.com
Team Ada member | "Digital Media Management"
Disclaimer: these are my opinions, not eMotion's.
Re: The root element is required... [was Re: An invalid XML character
(Unicode: 0x0)...]
Posted by Gary Kephart <ga...@emotion.com>.
Gary Kephart wrote:
> Gary Kephart wrote:
>
> > Eric Ye wrote:
> >
> > > The cause of your problem is that the file is encoded in "UTF-16" and there
> > > is no <?xml version="1.0" encoding="UTF-16" in the beginning file, so the
> > > parser will assume this is a UTF-8 encoded file(UTF-8 is the default
> > > encoding), that is why it sees byte 0x00 then throws the invalid Unicode
> > > exception.
> > >
> > > Two alternatives to solve this:
> > > 1. pre convert the file to UTF-8 encoding before pass it to the parser.
> > > 2. change the source to include <?xml version="1.0" encoding="UTF-16"?> at
> > > the very beginning.
>
> Ok, I caught my mistake. I was doing this:
>
> this.inputSource = new InputSource(new
> DataInputStream(this.socket.getInputStream()));
>
> when I should have been doing just this:
>
> this.inputSource = new InputSource(this.socket.getInputStream());
>
> Overcompensating for Unicode handling, I guess. Now I get this:
>
> org.xml.sax.SAXParseException: The root element is required in a well-formed
> document.
>
> Anyone know why? It seems to be unable to read anything from the stream, although
> I've checked that the client app is sending XML through the socket stream to my
> program.
Again, I found it. This is a note for all those using the MOS protocol
(http://www.enps.com/mosproto/): set the encoding for your input source to
"UnicodeBig".
Gary
--
Gary Kephart, Software Engineer | eMotion, Inc.
Phone: (310)914-2771 | 11835 West Olympic, Suite 1000
Fax: (310)479-8480 | Los Angeles, CA 90064
mailto:gary_kephart@acm.org | http://www.emotion.com
Team Ada member | "Digital Media Management"
Disclaimer: these are my opinions, not eMotion's.
Re: The root element is required... [was Re: An invalid XML character
(Unicode: 0x0)...]
Posted by Gary Kephart <ga...@emotion.com>.
Eric Ye wrote:
> Your new approach still didn't solve the Byte Order Mark problem.
>
> You have to convert the byte stream to be UTF-8 encoded and strip off the 2
> byte order mark bytes (0xFE 0xFF or 0xFF 0xFE) in the very beginning after
> the detection of byte order.
>
> or you 'll need to ask the source of the stream to add encoding decl, it
> would be a very reasonable request because the file you received from the
> socket is not even a well-formed xml file.
Thanks for your help. I'm glad there's people out there who help us XML newbies.
It seems, though, all I needed to do is call
inputStream.setEncoding("UnicodeBig")
and all of a sudden my program could read from the socket.
It looks as though Java already has some IO set up for this type of encoding. I
eventually found my answers in the following URLs:
http://grove.ufl.edu/usr/share/doclib/java/api/Package-sun.io.html
http://grove.ufl.edu/usr/share/doclib/java/api/sun.io.CharToByteUnicodeBig.html#_top_
http://grove.ufl.edu/usr/share/doclib/java/api/sun.io.CharToByteUnicodeBigUnmarked.html#_top_
Gary
--
Gary Kephart, Software Engineer | eMotion, Inc.
Phone: (310)914-2771 | 11835 West Olympic, Suite 1000
Fax: (310)479-8480 | Los Angeles, CA 90064
mailto:gary_kephart@acm.org | http://www.emotion.com
Team Ada member | "Digital Media Management"
Disclaimer: these are my opinions, not eMotion's.
Re: The root element is required... [was Re: An invalid XML character (Unicode: 0x0)...]
Posted by Eric Ye <er...@locus.apache.org>.
Your new approach still didn't solve the Byte Order Mark problem.
You have to convert the byte stream to be UTF-8 encoded and strip off the 2
byte order mark bytes (0xFE 0xFF or 0xFF 0xFE) in the very beginning after
the detection of byte order.
or you 'll need to ask the source of the stream to add encoding decl, it
would be a very reasonable request because the file you received from the
socket is not even a well-formed xml file.
_____
Eric Ye * IBM, JTC - Silicon Valley * ericye@locus.apache.org
----- Original Message -----
From: "Gary Kephart" <ga...@emotion.com>
To: <ga...@acm.org>
Cc: <xe...@xml.apache.org>
Sent: Wednesday, September 06, 2000 2:35 PM
Subject: The root element is required... [was Re: An invalid XML character
(Unicode: 0x0)...]
> Gary Kephart wrote:
>
> > Eric Ye wrote:
> >
> > > The cause of your problem is that the file is encoded in "UTF-16" and
there
> > > is no <?xml version="1.0" encoding="UTF-16" in the beginning file, so
the
> > > parser will assume this is a UTF-8 encoded file(UTF-8 is the default
> > > encoding), that is why it sees byte 0x00 then throws the invalid
Unicode
> > > exception.
> > >
> > > Two alternatives to solve this:
> > > 1. pre convert the file to UTF-8 encoding before pass it to the
parser.
> > > 2. change the source to include <?xml version="1.0"
encoding="UTF-16"?> at
> > > the very beginning.
>
> Ok, I caught my mistake. I was doing this:
>
> this.inputSource = new InputSource(new
> DataInputStream(this.socket.getInputStream()));
>
> when I should have been doing just this:
>
> this.inputSource = new InputSource(this.socket.getInputStream());
>
> Overcompensating for Unicode handling, I guess. Now I get this:
>
> org.xml.sax.SAXParseException: The root element is required in a
well-formed
> document.
>
> Anyone know why? It seems to be unable to read anything from the stream,
although
> I've checked that the client app is sending XML through the socket stream
to my
> program.
>
> Thanks in advance,
> Gary
>
> >
> >
> > > Eric Ye * IBM, JTC - Silicon Valley * ericye@locus.apache.org
> > >
> > > ----- Original Message -----
> > > From: "Gary Kephart" <ga...@emotion.com>
> > > To: <xe...@xml.apache.org>
> > > Sent: Friday, September 01, 2000 4:05 PM
> > > Subject: An invalid XML character (Unicode: 0x0)...
> > >
> > > > ... was found in the prolog of the document.
> > > >
> > > > This is the error I'm getting. Attached is the XML file. I'm using
JDK
> > > 1.2.2
> > > > and Xerces 1.1.3. There's no DTD file. Below are excerpts from my
code.
> > > I'm
> > > > feeding the XML file into a client program, and it's sending the XML
over
> > > a
> > > > socket. The client program was written in C++ (but not by me).
> > > > What, in general, are the reasons this exception is thrown? How do I
get
> > > > around it?
> > > >
> > > > Socket clientSocket =
this.serverSocket.accept();
> > > > this.inputSource = new InputSource(new
> > > > DataInputStream(this.socket.getInputStream()));
> > > > this.saxParser.parse(this.inputSource);
>
>
>
>
> --
> Gary Kephart, Software Engineer | eMotion, Inc.
> Phone: (310)914-2771 | 11835 West Olympic, Suite 1000
> Fax: (310)479-8480 | Los Angeles, CA 90064
> mailto:gary_kephart@acm.org | http://www.emotion.com
> Team Ada member | "Digital Media Management"
>
> Disclaimer: these are my opinions, not eMotion's.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-j-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-j-dev-help@xml.apache.org
>
>