Here are three standard API methods to produce ISO-8859-1 encoded documents.
Using the StAX API:
// output stream
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
// transcode
StringReader xml = new StringReader("<x>pi: \u03A0</x>");
XMLEventReader reader = XMLInputFactory.newFactory().createXMLEventReader(
xml);
XMLEventWriter writer = XMLOutputFactory.newFactory().createXMLEventWriter(
buffer, "ISO-8859-1");
try {
writer.add(reader);
} finally {
writer.close();
}
// proof
String decoded = new String(buffer.toByteArray(),
Charset.forName("ISO-8859-1"));
System.out.println(decoded);
Using the DOM API:
// output stream
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
// create XML DOM
InputSource src = new InputSource(new StringReader("<x>pi: \u03A0</x>"));
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()
.parse(src);
// serialize
DOMImplementationLS impl = (DOMImplementationLS) doc.getImplementation();
LSOutput out = impl.createLSOutput();
out.setEncoding("ISO-8859-1");
out.setByteStream(buffer);
impl.createLSSerializer().write(doc, out);
// proof
String decoded = new String(buffer.toByteArray(),
Charset.forName("ISO-8859-1"));
System.out.println(decoded);
Using the transform package:
// output stream
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
// transformation
StreamSource src = new StreamSource(new StringReader("<x>pi: \u03A0</x>"));
StreamResult res = new StreamResult(buffer);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
transformer.transform(src, res);
// proof
String decoded = new String(buffer.toByteArray(),
Charset.forName("ISO-8859-1"));
System.out.println(decoded);
Which you would use depends on your use case; the StAX API is probably the most efficient.
All this sample code will emit documents equivalent to:
<?xml version="1.0"?><x>pi: Π</x>