import org.w3c.dom.bootstrap.DOMImplementationRegistry; import org.w3c.dom.DOMImplementation; import org.w3c.dom.Document; import org.w3c.dom.DocumentType; import org.w3c.dom.ls.DOMImplementationLS; import org.w3c.dom.ls.LSSerializer; import org.w3c.dom.ls.LSOutput; import java.io.FileOutputStream; public class DOMDoctype1 { /** * @param args the command line arguments */ public static void main(String[] args) { try { DOMImplementation implementation = DOMImplementationRegistry.newInstance().getDOMImplementation("XML 3.0 Core 3.0"); DocumentType docType = implementation.createDocumentType("FICHES", null, "docform.dtd"); Document doc = implementation.createDocument(null, "FICHES", docType); doc.insertBefore(doc.createProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"stylesheet.xsl\""), doc.getDocumentElement()); DOMImplementationLS lsImp = (DOMImplementationLS)implementation; LSSerializer lsSer = lsImp.createLSSerializer(); System.out.println(lsSer.writeToString(doc)); for (int i = 0; i < doc.getChildNodes().getLength(); i++) { System.out.println(i + ": " + doc.getChildNodes().item(i).getNodeType()); } FileOutputStream outputFile1 = new FileOutputStream("test1.xml"); LSOutput dest1 = lsImp.createLSOutput(); dest1.setByteStream(outputFile1); lsSer.write(doc, dest1); } catch (Exception e) { e.printStackTrace(); } } }