Why is DocType null after transformation?

246 views Asked by At

I receive a document finalDocument and want to set the same DocType as the input document xmlDocument. This is how I do it:

finalDocument = icBuilder.parse(new InputSource(new ByteArrayInputStream(xmlString.getBytes("UTF-8"))));

Transformer transformer = TransformerFactory.newInstance().newTransformer();

DocumentType doctype = xmlDocument.getDoctype();

StringWriter writer = new StringWriter();

transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());    

transformer.transform(new DOMSource(finalDocument), new StreamResult(writer));

However, for some reason the DocType of finalDocument is not set. I don't get an exception or anything - it's just null.

Any idea what I am doing wrong?

Btw: doctype.getSystemId() and doctype.getPublicId() are not null and valid.

1

There are 1 answers

1
Patrick Koorevaar On BEST ANSWER

Your finalDocument variable is passed as a source to the transform method:

void javax.xml.transform.Transformer.transform(Source xmlSource, Result outputTarget) throws TransformerException

The result of the transformation is placed in the second parameter. So I would expect that the docType's you have set with the setOutputProperty will be placed on the target, which is your writer object.