The document discusses using an OutputStreamWriter instead of a StringWriter when serializing XML with JAXP to properly handle character encoding. It provides an example of using an OutputStreamWriter wrapped around a ByteArrayOutputStream, setting the encoding to "UTF-8", and passing it to the StreamResult to correctly serialize XML that may contain international characters.
The document discusses using an OutputStreamWriter instead of a StringWriter when serializing XML with JAXP to properly handle character encoding. It provides an example of using an OutputStreamWriter wrapped around a ByteArrayOutputStream, setting the encoding to "UTF-8", and passing it to the StreamResult to correctly serialize XML that may contain international characters.
The document discusses using an OutputStreamWriter instead of a StringWriter when serializing XML with JAXP to properly handle character encoding. It provides an example of using an OutputStreamWriter wrapped around a ByteArrayOutputStream, setting the encoding to "UTF-8", and passing it to the StreamResult to correctly serialize XML that may contain international characters.
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