You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Carl Hume <ch...@dna13.com> on 2004/01/14 21:52:17 UTC

[OT] Character Sets (Was Re: ContentHander characters discrepancy)

On January 14, 2004 03:34 pm, Bob Foster wrote:
>
> Of course not. The default encoding is UTF-8. An erroneous encoding
> declaration won't make things better. I mean, specify the actual encoding.
>
I'm going to take a step back for a second, because I'm (obviously) confused 
on several fronts.

When I receive the xml file, it currently specifies a UTF-8 encoding.  Some 
characters are invalid for UTF-8, so are interpreted based on the locale 
settings of the operating system.  This interpretation can be avoided by 
using the correct character set for the interpretation, and specifying it 
instead of UTF-8.

Could someone point me to a reference / tutorial for deciding the appropriate 
character set to use?  Christopher mentioned Cp 1252 in an earlier email.  
Would that be more appropriate?

Thanks for all the help.

Cheers!
Carl


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: [OT] Character Sets (Was Re: ContentHander characters discrepancy)

Posted by Andy Clark <an...@apache.org>.
Carl Hume wrote:
> When I receive the xml file, it currently specifies a UTF-8 encoding.  Some 
> characters are invalid for UTF-8, so are interpreted based on the locale 
> settings of the operating system.  This interpretation can be avoided by 
> using the correct character set for the interpretation, and specifying it 
> instead of UTF-8.

No, this is incorrect -- all Unicode characters can be
represented by UTF-8. But don't feel too bad, encodings
are always a tricky beast to master. In XML, as with other
applications of i18n, you have to keep various things
straight.

First, in Java and XML, a character refers to a single
codepoint ("character") in the Unicode codepage ("set of
characters"). An encoding is a stream of byte(s) that
represent a character. There are encodings such as UTF-8
that are able to represent all characters in the Unicode
codepage. Other encodings can only represent specific
characters. For example, ShiftJIS can encode Japanese
characters but cannot represent Hindi characters.

Second, in XML, specifying UTF-8 for the encoding in the
XML declaration (e.g. "<?xml version='1.0' encoding='...'?>)
does *not* mean that the file is actually saved in that
encoding! The code that saved that file is responsible for
ensuring that it was saved in the encoding that matches
that specified in the XML declaration.

In the absence of a specified encoding, XML assumes UTF-8
(unless it can be auto-detected from the first few bytes
of the file -- e.g. UTF-16, EBCDIC, etc).

Third, in Java, you should always specify an encoding when
creating an InputStreamReader or an OutputStreamWriter. If
you don't, then the system's default locale will be used to
determine the encoding to use, as someone already stated.
This happens silently and can corrupt data without you
knowing. When parsing XML, though, it's best to let the
parser do the encoding detection -- so use InputStream
instead of Reader.

> Could someone point me to a reference / tutorial for deciding the appropriate 
> character set to use?  Christopher mentioned Cp 1252 in an earlier email.  
> Would that be more appropriate?

The encoding "Cp1252" (also known as "Windows-1252") is
the default for English Windows platforms. But I wouldn't
suggest that you create files in that encoding because it
cannot represent all Unicode characters. I recommend
using UTF-8 (or UTF-16) unless you know specifically that
the document contains certain characters that can be
represented in your encoding of choice.

-- 
Andy Clark * andyc@apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: [OT] Character Sets (Was Re: ContentHander characters discrepancy)

Posted by Andy Clark <an...@apache.org>.
Bob Foster wrote:
> BTW, my handy table says the IANA name for CP-1252 is "WINDOWS-1252" 
> while the Java name for it is "Cp1252". I would hope Xerces accepts 
> either one.

The XML spec points to IANA names for encodings so that is
what Xerces uses. There is a feature, though, to expliticly
allow you to use Java encoding names but you should use
this with care as your documents may no longer be portable.

-- 
Andy Clark * andyc@apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org


Re: [OT] Character Sets (Was Re: ContentHander characters discrepancy)

Posted by Bob Foster <bo...@objfac.com>.
Carl Hume wrote:

> On January 14, 2004 03:34 pm, Bob Foster wrote:
> 
>>Of course not. The default encoding is UTF-8. An erroneous encoding
>>declaration won't make things better. I mean, specify the actual encoding.
>>
> 
> I'm going to take a step back for a second, because I'm (obviously) confused 
> on several fronts.
> 
> When I receive the xml file, it currently specifies a UTF-8 encoding.  Some 
> characters are invalid for UTF-8, so are interpreted based on the locale 
> settings of the operating system.  This interpretation can be avoided by 
> using the correct character set for the interpretation, and specifying it 
> instead of UTF-8.
> 
> Could someone point me to a reference / tutorial for deciding the appropriate 
> character set to use?  Christopher mentioned Cp 1252 in an earlier email.  
> Would that be more appropriate?
> 
> Thanks for all the help.

And you seem to be getting a lot of it! ;-}

I don't know that any reference / tutorial is going to tell you what 
character set is used to write a document, since that depends on the 
program that writes it and the platform the program runs on. CP-1252 is 
the default encoding for Windows US English systems, so if the program 
is running on one of these and it doesn't do anything special about the 
encoding, chances are that's what it is writing. But from this distance, 
that is just speculation.

Anyway, the way to fix this problem is to fix the program that writes 
the document so it writes an xml declaration to match the encoding it 
writes the document in. (If that sentence isn't too confusing.)

BTW, my handy table says the IANA name for CP-1252 is "WINDOWS-1252" 
while the Java name for it is "Cp1252". I would hope Xerces accepts 
either one.

Bob Foster
http://xmlbuddy.com/


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-j-user-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-j-user-help@xml.apache.org