How to create an empty DOCTYPE using W3C DOM in Java?

ipdzcngniv

New Member
I am trying to read an XML document and output it into a new XML document using the W3C DOM API in Java. To handle DOCTYPEs, I am using the following code (from an input Document \[code\]doc\[/code\] to a target File \[code\]target\[/code\]):\[code\]TransformerFactory transfac = TransformerFactory.newInstance();Transformer trans = transfac.newTransformer();trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); // omit '<?xml version="1.0"?>'trans.setOutputProperty(OutputKeys.INDENT, "yes");// if a doctype was set, it needs to persistif (doc.getDoctype() != null) { DocumentType doctype = doc.getDoctype(); trans.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId()); trans.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());}FileWriter sw = new FileWriter(target);StreamResult result = new StreamResult(sw);DOMSource source = new DOMSource(doc);trans.transform(source, result);\[/code\]This works fine for both XML documents with and without DOCTYPEs. However, I am now coming across a \[code\]NullPointerException\[/code\] when trying to transform the following input XML document:\[code\]<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE permissions ><permissions> // ...</permissions>\[/code\]HTML 5 uses a similar syntax for its DOCTYPEs, and it is valid. But I have no idea how to handle this using the W3C DOM API - trying to set the \[code\]DOCTYPE_SYSTEM\[/code\] to \[code\]null\[/code\] throws an exception. Can I still use the W3C DOM API to output an empty doctype?
 
Back
Top