You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Laurent ForĂȘt <l....@clipack.com> on 2004/08/25 15:58:42 UTC
Encoding problem with using SAXSource and Xalan Transformer.
Hello,
I have a String containing an xml document with iso-8859-1 encoding. I would like to transform it
without using an xsl but with an XMLFilterImpl.
Then I have the following code :
Transformer transformer_ = new org.apache.xalan.processor.TransformerFactoryImpl().newTransformer();
SAXParserFactory factory_ = new org.apache.xerces.jaxp.SAXParserFactoryImpl().newInstance();
SAXParser saxParser = factory_.newSAXParser();
filter.setParent(saxParser.getXMLReader());
InputSource is = new InputSource(new ByteArrayInputStream(acXmlConf.getBytes("ISO8859_1")));
is.setEncoding("ISO8859_1");
SAXSource source = new SAXSource(filter, is);
//SAXSource source = new SAXSource(is);
logger_.debug("check IS encoding :"+source.getInputSource().getEncoding());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
java.io.OutputStreamWriter osw = new java.io.OutputStreamWriter(baos,"ISO8859_1");
logger_.debug("osw encoding :"+osw.getEncoding());
StreamResult sr = new StreamResult(osw);
transformer_.transform(source, sr);
I have for a result an xml with the default encoding UTF-8 instead the one I expect, I mean
iso-8859-1. I have the same result even if I don't use the filter (see the commented line).
What have I done wrong ?
Laurent F.
Re: Encoding problem with using SAXSource and Xalan Transformer.
Posted by Henry Zongaro <zo...@ca.ibm.com>.
Hi, Laurent.
Laurent ForĂȘt <l....@clipack.com> wrote on 08/25/2004 09:58:42 AM:
> I have a String containing an xml document with iso-8859-1 encoding.
> I would like to transform it
> without using an xsl but with an XMLFilterImpl.
>
> Then I have the following code :
>
> Transformer transformer_ = new org.apache.xalan.processor.
> TransformerFactoryImpl().newTransformer();
...
> ByteArrayOutputStream baos = new ByteArrayOutputStream();
> java.io.OutputStreamWriter osw = new java.io.
> OutputStreamWriter(baos,"ISO8859_1");
> logger_.debug("osw encoding :"+osw.getEncoding());
> StreamResult sr = new StreamResult(osw);
>
> transformer_.transform(source, sr);
>
>
> I have for a result an xml with the default encoding UTF-8 instead
> the one I expect, I mean
> iso-8859-1. I have the same result even if I don't use the filter
> (see the commented line).
The Transformer object does not check the output encoding specified
on the OutputStream contained in the StreamResult object - it's guided
entirely by the output properties specified either in the stylesheet or on
the Transformer object. You can try setting the encoding output property
on the Transformer object as follows:
transformer.setOutputProperty(
javax.xml.transform.OutputKeys.ENCODING,
"ISO8859_1");
I hope that helps.
Thanks,
Henry
------------------------------------------------------------------
Henry Zongaro Xalan development
IBM SWS Toronto Lab T/L 969-6044; Phone +1 905 413-6044
mailto:zongaro@ca.ibm.com