wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Invalid byte 1 of 1-byte UTF-8 sequence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Invalid byte 1 of 1-byte UTF-8 sequence" Watch "Invalid byte 1 of 1-byte UTF-8 sequence" New topic
Author

Invalid byte 1 of 1-byte UTF-8 sequence

rajanikanth bhagavan kanth
Ranch Hand

Joined: May 20, 2008
Posts: 78
Hi All,
In my application while parsing the xml im getting this error.

Invalid byte 1 of 1-byte UTF-8 sequence

Is this error due to my code problem??
I'm getting the xml file with some third party API.

Regards
Bhagavan R
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by rajanikanth bhagavan kanth:
Is this error due to my code problem??
Probably. The error message says that you are reading the XML using the UTF-8 encoding, but it was produced using some other encoding, I think. You should first check that when you give the XML document to the parser, you don't enforce an encoding that might be incorrect by using a Reader of any kind. Use an InputStream if at all possible.

Or it's possible that the person who produced the XML document made that kind of error, and it is really malformed. But if you have to ask, it's a good idea to check your code first. Post your parsing code if you like.
rajanikanth bhagavan kanth
Ranch Hand

Joined: May 20, 2008
Posts: 78

Here is my code..Its working fine some times but getting error some times.defnetly error due to thirdparty xml file but i want to kow how to fix that one

Thnks
[ November 27, 2008: Message edited by: Paul Sturrock ]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Your code would be more readable if you enclosed in the "CODE" tags. (There's a button for creating them.) Let me remind you, I asked you to check whether you were using Readers or InputStreams or what as input to your parser, didn't I? So... which is it? Have a look at the code you posted and see if you can tell from that.

Does that help? If not, post some relevant code.
[ November 26, 2008: Message edited by: Paul Clapham ]
rajanikanth bhagavan kanth
Ranch Hand

Joined: May 20, 2008
Posts: 78
Yes im passing InputStream as input for that parser




So can you please tell me what is the mistake in my code

Regards
Bhagavan
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Oh, you are getting the data from HTTP. There's a special rule in that case: the charset you should use is the charset declared on the response and not the charset declared in the document.

So the first thing you have to do is find the response's charset. Go through the headers from the response and you might find something like this:
Content-Type: text/xml; charset=windows-1252

Extract the charset from it. Then wrap your InputStream in an InputStreamReader with that charset and pass the InputStreamReader to the parser.
rajanikanth bhagavan kanth
Ranch Hand

Joined: May 20, 2008
Posts: 78
Hi,
I will try the same thing and let you the status.

Regards,
Bhagavan R.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Invalid byte 1 of 1-byte UTF-8 sequence