Hi
Pavel,
I had
a similar problem before and got help from the list, here is the suggested
solution that works.
Hi Rajesh,
you have to do 2 things:
- create a Marshaller and configure it with a
Serializer that honors your encoding (the
default
Marshaller uses a default Serializer which seems
to
assume ASCII and therefore uses the escape
sequences)
- when you write to a file make sure that the stream
writer
is also configured to use your encoding, otherwise
you
may loose characters
In the following example I use UTF-8:
// create stream writer
for writing to file with right encoding
FileOutputStream fos1 =
new
FileOutputStream("out1.xml");
OutputStreamWriter osw1 = new OutputStreamWriter( fos1, "UTF-8");
OutputStreamWriter osw1 = new OutputStreamWriter( fos1, "UTF-8");
// create serializer
that is aware of encoding
org.apache.xml.serialize.OutputFormat format =
org.exolab.castor.util.Configuration.getOutputFormat();
format.setEncoding("UTF-8");
org.apache.xml.serialize.Serializer serializer =
org.exolab.castor.util.Configuration.getSerializer();
serializer.setOutputFormat(format);
serializer.setOutputCharStream(osw1);
org.exolab.castor.util.Configuration.getOutputFormat();
format.setEncoding("UTF-8");
org.apache.xml.serialize.Serializer serializer =
org.exolab.castor.util.Configuration.getSerializer();
serializer.setOutputFormat(format);
serializer.setOutputCharStream(osw1);
// create marshaller
with the created serializer
org.xml.sax.DocumentHandler docHandler =
serializer.asDocumentHandler();
org.exolab.castor.xml.Marshaller marsh = new org.exolab.castor.xml.Marshaller(docHandler);
marsh.marshal(myroot);
org.exolab.castor.xml.Marshaller marsh = new org.exolab.castor.xml.Marshaller(docHandler);
marsh.marshal(myroot);
In the other direction (unmarshalling) you need to make sure that your
reader is
also aware of the encoding that is used.
Cheers,
Laslo
======================================================================
__
Laslo Bednarik Tel. {0|49}-7031-626-1184
___ /
/__ ___ _ __ WWW Application
Telnet 702-1184
/
-_)_/ _ \/ _ \ |/|/ / Devel. Engineer Fax
{0|49}-7031-626-1182
\__(_)_//_/\___/__,__/
702-1182
========== Hewlett-Packard
===========
__
= e!now - Customer Experience
SL = ___ / /__ ___ _
__
= Mail: [EMAIL PROTECTED]
= / -_)_/ _ \/ _ \ |/|/
/
=....................................=
\__(_)_//_/\___/__,__/
======================================================================
From: Pavel Hlobil
[mailto:[EMAIL PROTECTED]]
Sent: Sunday, March 24, 2002 7:15 PM
To: [EMAIL PROTECTED]
Subject: [castor-dev] un_marshal and UTF-8
Sent: Sunday, March 24, 2002 7:15 PM
To: [EMAIL PROTECTED]
Subject: [castor-dev] un_marshal and UTF-8
I have the following problem.
With a xml file that has foreign characters like below. (encoded in UTF-8.)
<Name>���X�^���g���b�Z</Name>
castor outputs after modifications to the file
<Name> לך את הקישוריות</Name>
Below is a sample of my java code.
// Marshal out the data to the same XML file.
xml.marshal(new OutputStreamWriter(
new FileOutputStream(fileName), "UTF-8"));
I have even used FileWriter.
Can someone help. Can I fix this.
----------------------------------------------------------- If you wish to unsubscribe from this mailing, send mail to [EMAIL PROTECTED] with a subject of: unsubscribe castor-dev
