You are on page 1of 2

comp.lang.java.

programmer: Re: Serializing XML with JAXP − help needed

Re: Serializing XML with JAXP − help needed

Source: http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2004−02/2660.html

From: Michael Berg (michael.berg_at_bergconsult.dot.com)


Date: 02/22/04

Date: Sun, 22 Feb 2004 19:07:10 +0100

Hi all,

The problem is related to the use of a StringWriter to collect the XML


output. Apparently StringWriters have their own idea about character
encoding, so use an OutputStreamWriter in stead − like this, for example:

java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();


javax.xml.transform.stream.StreamResult result = new
javax.xml.transform.stream.StreamResult(
new java.io.OutputStreamWriter(
baos,
"UTF−8"
)
);

/Michael
www.hyperpal.com

"Michael" <michael.berg@bergconsult.dot.com (figure it out)> wrote in


message news:40380891$0$95001$edfadb0f@dread11.news.tele.dk...
> Hi all,
>
> I'm trying to serialize an xml document with JAXP. The xml may or may not
> contain international characters, and so I want any text elements to be
> UTF−8 encoded. Consider the following (a brief summary is included below
the
> code):
>
> −−−− code begin −−−−
>
> org.w3c.dom.Document doc =
>
javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().
> newDocument();
>
> org.w3c.dom.Element el = doc.createElement("element");
> el.setAttribute("attr1","attr1value");
> el.appendChild(doc.createTextNode("Danish < æøå > characters!"));

Re: Serializing XML with JAXP − help needed 1


comp.lang.java.programmer: Re: Serializing XML with JAXP − help needed
> doc.appendChild(el);
>
> javax.xml.transform.TransformerFactory transformerFactory =
> javax.xml.transform.TransformerFactory.newInstance();
> javax.xml.transform.Transformer transformer =
> transformerFactory.newTransformer();
>
>
transformer.setOutputProperty(javax.xml.transform.OutputKeys.INDENT,"yes");
>
transformer.setOutputProperty("{http://xml.a

Re: Serializing XML with JAXP − help needed 2

You might also like