cancel
Showing results for 
Search instead for 
Did you mean: 

Output encoding in Java mapping ?

Former Member
0 Kudos

Hello,

I have written a Java mapping which reads an input document and creates an output document. My problem is, the German special characters are lost in the output document (wrong characters). This is strange, because I am only using regular DOM methods for adding the output nodes and values. Anybody has an idea where I have to explicitly define the output encoding ? XI automatically uses UTF-8, I am using the StandardDOMWriter class of the com.inqmy.lib.xml package from XI, and DocumentBuilder from javax.xml.parsers.

But the output is wrong nevertheless. I think the encoding is already set in the OutputStream input variable of the execute methods. So no need to be changed (?)

Code sample below:

public void execute(InputStream in, OutputStream out) throws StreamTransformationException {

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

Document idoc = documentBuilder.parse(in); // input document

Document odoc = documentBuilder.newDocument(); // output document

// create some elements and values with helper method below

// [..]

// write output document to stream

odoc.appendChild(oRoot);

StandardDOMWriter sdw = new StandardDOMWriter();

sdw.write(odoc, out, null);

}

private Element createElement(String elementName, String value, Element parent, Document doc) {

Element element = createElement(elementName, parent, doc);

// here we put the input content to output value

element.appendChild(doc.createTextNode(value));

// ok, special character still there:

trace.addInfo("Read back: " + getNodeValue(element));

return element;

}

CSY

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

I solved the problem. For some reason, the inqmy parser does not work correctly. If I remember correctly, the SapXMLToolkit (from where it comes), is deprecated anyway.

Instead I use now standard JAXP transform streaming, and that works fine:

// commented code does not convert german special characters correctly:

//StandardDOMWriter sdw = new StandardDOMWriter();

//sdw.write(odoc, out, null);

// Serialisation through Transform.

DOMSource domSource = new DOMSource(odoc);

StreamResult streamResult = new StreamResult(out);

TransformerFactory tf = TransformerFactory.newInstance();

Transformer serializer = tf.newTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");

serializer.setOutputProperty(OutputKeys.INDENT,"yes");

serializer.transform(domSource, streamResult);

CSY