How to add DOCTYPE in XML using jaxp-1.2 ?
Hi all,
I know this question has been asked at least 2000 times. But I couldn't find the answer from the previously posted messages.
I would like to create a new Document (actually a XML document) and to write it out into a file. Of course, to be useful, that document needs a DTD... But I can't find a way to create thatDOCTYPE tag.
In fact, I get this error: org.apache.crimson.tree.DomEx: HIERARCHY_REQUEST_ERR: This node isn't allowed there.
at org.apache.crimson.tree.XmlDocument.appendChild(XmlDocument.java:661)
at ServiceManager.PlaylistDOM.createDoc(PlaylistDOM.java:99)
at ServiceManager.PlaylistDOM.load(PlaylistDOM.java:52)
at java.lang.Thread.run(Thread.java:536)
The code I have is the following:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try{
//create a factory and configure it
DocumentBuilder builder = factory.newDocumentBuilder();
playlistDoc = builder.newDocument();
DOMImplementation impl = builder.getDOMImplementation();
DocumentType doctype = impl.createDocumentType(ROOT_EL, null,"playlist.dtd");
playlistDoc = impl.createDocument(null, ROOT_EL, doctype);
}
catch (ParserConfigurationException pce){}
//create the root of the XML file
Element root = (Element) playlistDoc.createElement(ROOT_EL);
root.setAttribute(FRAME_ATTR, fNb);
playlistDoc.appendChild(root);
//now print it out into XML file
TransformerFactory tfactory = TransformerFactory.newInstance();
try{
Transformer transformer = tfactory.newTransformer();
DOMSource source =new DOMSource(playlistDoc.getDocumentElement());
StreamResult res =new StreamResult(new File(filename));
transformer.transform(source, res);
}
catch (TransformerConfigurationException tce){}
catch (TransformerException te){}
Thank you for your help

