Special letters in European languages
Hi all
I have the following problem...
I have an URL to get XML data from a webservice.
In the XML, some words include french letters like accented "e" ( ?)
After I read XML data via URL, this special letter is replaced with a few absurd letters ( 脙漏 ).
Here is how I read data from the URL:
publicstatic StringBuffer getDataFromUrl(String urlString)throws RuntimeException{
StringBuffer content =new StringBuffer();
try{
URL url=new URL(urlString);
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
int ch;
while( (ch=is.read())!=-1 )
content.append( (char)ch );
is.close();
}
catch ( Exception e ){
e.printStackTrace();
}
return content;
}
If I open the URL in web browser, the French letters are displayed correctly.
Thanks in advance for your answers.
[1459 byte] By [
dromanyuka] at [2007-11-27 2:10:48]

Presumably the XML is encoded in UTF-8, but you are ignoring that and using your own hack for decoding bytes into chars, which is equivalent to assuming ISO-8859-1. Here's the basic I/O tutorial which explains byte streams versus character streams:
http://java.sun.com/docs/books/tutorial/essential/io/
However if you are reading XML data from a web service you should really be using an XML parser to do that. It takes care of discovering the XML document's encoding and applying it correctly. Read this tutorial which explains everything you don't know yet about character encoding in Java with special reference to XML:
http://skew.org/xml/tutorial/
Thanks!
That's how I fixed it and it works OK:
public static StringBuffer getDataFromUrl(String urlString) throws RuntimeException{
StringBuffer result= new StringBuffer();
try{
URL url=new URL(urlString);
URLConnection con = url.openConnection();
InputStream is = con.getInputStream();
BufferedReader reader = new BufferedReader( new InputStreamReader( is, "UTF8" ) ); // !!!
String line = reader.readLine();
while (line != null) {
result.append(line + '\n');
line = reader.readLine();
}
reader.close();
return result;
}
catch ( Exception e ){
e.printStackTrace();
}
return result;
}