+ Start a Discussion

XmlStreamWriter bug: unable to specify encodings other than UTF-8?

I'm trying to use the XmlStreamWriter class to create an Xml document for to be used to generate a REST API call to a third party API. The API expects ISO-8859-1 character encoding. I build my Xml document as follows:



String camNs = 'http://api.xxxxxx.com/xsd/xxxxx';
String camPrefix = 'cam';
XmlStreamWriter w = new XmlStreamWriter();
w.writeStartDocument('ISO-8859-1', '1.0');
w.writeStartElement(camPrefix, 'campaign', camNs);
w.writeNamespace(camPrefix, camNs);


String xmlOutput = w.getXmlString();
return xmlOutput;


When I try to execute this code I get the following error: System.XmlException: Underlying stream encoding 'UTF-8' and input paramter for writeStartDocument() method 'ISO-8859-1' do not match.


According to the Apex docs, the Apex XmlStreamWriter class is based on the Stax API. The javadocs for the Stax API state the following for the XmlStreamWriter.writeStartDocument(String encoding, String version) method:


Note that the encoding parameter does not set the actual encoding of the underlying output. That must be set when the instance of the XMLStreamWriter is created using the XMLOutputFactory.

Unfortunately, the Apex implementation only has a no-argument constructor for the XmlStreamWriter class, so how can I set the encoding for the underlying stream when the XmlStreamWriter instance is created? It seems to me that the Apex implementation is limited to UTF-8 encoding.





Laurent LemazurierLaurent Lemazurier



I've got exactly the same issue.


Did you find a way to solve it?