Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / August 2006

Tip: Looking for answers? Try searching our database.

Transformer encoding not working for ISO-8859-1 only for UTF-8

Thread view: 
janib - 07 Aug 2006 10:44 GMT
I have a problem when transforming text containing the swedish letters
"å", "ä" and "ö". If I do

Transformer t =TransformerFactory.newInstance().newTransformer();
t.setOutputProperty( OutputKeys.METHOD, "xml");
t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
t.setOutputProperty( OutputKeys.INDENT, "yes");
t.setOutputProperty( OutputKeys.ENCODING, "ISO-8859-1");   <------- *
t.transform( new DOMSource( document), new StreamResult( output ) );
return output.toString( );

I get an xml-file containing broken characters (=?) for the swedish
letters:

<?xml version="1.0" encoding="ISO-8859-1"?>
...
<channelinfo confirmed="true" validate="false" name="Internet">
     <publishdate>1154940455898</publishdate>
     <unpublishdate>1154940455898</unpublishdate>
     <attribute name="rooms"/>
     <attribute name="year"/>
     <attribute name="title">K?pes</attribute>  <------------- *
     <attribute name="price">20000</attribute>
     <attribute name="area"/>
     <attribute name="body">Vill k?pa en truck</attribute>
<-------------- *
</channelinfo>

but if I change the encoding to UTF-8:

t.setOutputProperty( OutputKeys.ENCODING, "UTF-8");   <------- *

the letters are alright:

<?xml version="1.0" encoding="UTF-8"?>
...
<channelinfo confirmed="true" validate="false" name="Internet">
     <publishdate>1154940455898</publishdate>
     <unpublishdate>1154940455898</unpublishdate>
     <attribute name="rooms"/>
     <attribute name="year"/>
     <attribute name="title">Köpes</attribute>  <------------- *
     <attribute name="price">20000</attribute>
     <attribute name="area"/>
     <attribute name="body">Vill köpa en truck</attribute>
<-------------- *
</channelinfo>

But the xml has to be formated in ISO-8859-1 so it would be nice if I
could make it work with that encoding.

Anyone know where I can alter this behavior or why it behaves like
above?
Jono - 07 Aug 2006 11:31 GMT
Hi Janib,
Your code works fine for me (as expected, because å", "ä" and "ö"
are part of the ISO-8859-1 character set), so I think the problem might
lie with one of the objects you're creating out of the scope of the
code snippet. Your "output" object might have a side-effect if it's
doing some character encoding of its own. I tried with a StringWriter
and also with a FileOutputStream and it worked correctly (using Java
1.5).
Cheers,
Jono

> I have a problem when transforming text containing the swedish letters
> "å", "ä" and "ö". If I do
[quoted text clipped - 49 lines]
> Anyone know where I can alter this behavior or why it behaves like
> above?
janib - 07 Aug 2006 12:35 GMT
Tje output object is only a ByteArrayOuputStream...

ByteArrayOutputStream output = new ByteArrayOutputStream( );

Jono skrev:

> Hi Janib,
> Your code works fine for me (as expected, because å", "ä" and "ö"
[quoted text clipped - 60 lines]
> > Anyone know where I can alter this behavior or why it behaves like
> > above?
Roland de Ruiter - 07 Aug 2006 14:15 GMT
> Tje output object is only a ByteArrayOuputStream...
>
> ByteArrayOutputStream output = new ByteArrayOutputStream( );

See my reply in comp.lang.java.help
Signature

Regards,

Roland



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.