Accessing the DocumentType of a Document

Sample xml file:


<?xml version="1.0" ?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.docbook.org/xml/4.2/docbookx.dtd">
<article>
  <title>Test article</title>
  <para>Test paragraph</para>
</article>
  

Assuming you have successfully parsed and loaded the xml file into a Document object, you can access the Document Type using the DocumentType object.


  Document doc;
  ...
  // assume that the doc has been created above.  
  DocumentType doctype = doc.getDoctype();
  System.out.println("doctype.getPublicId(): " + doctype.getPublicId());
  System.out.println("doctype.getSystemId(): " + doctype.getSystemId());
  System.out.println("doctype.getName(): " + doctype.getName());
  

The output of the above program would be:


  doctype.getPublicId(): -//OASIS//DTD DocBook XML V4.2//EN
  doctype.getSystemId(): http://www.docbook.org/xml/4.2/docbookx.dtd
  doctype.getName(): article
  

Published: Monday, 16 August 2004