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.
Your
finalDocumentvariable is passed as a source to thetransformmethod:The result of the transformation is placed in the second parameter. So I would expect that the docType's you have set with the
setOutputPropertywill be placed on the target, which is your writer object.