[This local archive copy mirrored from the .ZIP file at the canonical site: http://home.iclweb.com/icl2/mhkay/dtdgen.zip, 1998-05-05; get the source via this URL.]

DTDGen - XML DTD Generator


DTDGen is a program that takes an XML document as input and produces a Document Type Definition (DTD) as output.

The aim of the program is to give you a quick start in writing a DTD. The DTD is one of the many possible DTDs to which the input document conforms. Typically you will want to examine the DTD and edit it to describe your intended documents more precisely. In a few cases you will have to edit the DTD before you can use it.


First install a parser and, if necessary, the SAX Driver for that parser. Details of SAX parsers are available at www.microstar.com

Install the supplied class files in a directory on your CLASSPATH.

From the command line, enter:

java -Dsax.parser=parser DTDGen inputfile >outputfile

The input file must be an XML document; typically it will have no DTD. If it does have a DTD, the DTD will be used by the parser but it will be ignored by the DTDGen utility.

The parser parameter must be the name of a class that implements the SAX interface.

The output file will be an XML external document type definition.

The input file is not modified; if you want to edit it to refer to the generated DTD, you must do this yourself.

What it does

The program makes a list of all the elements and attributes that appear in your document, noting how they are nested, and noting which elements contain character data.

When the document has been completely processed, the DTD is generated according to the following rules:

The resulting DTD will often contain rules that are either too restrictive or too liberal. The DTD may be too restrictive if it prohibits constructs that do not appear in this document, but might legitimately appear in others. It may be too liberal if it fails to detect patterns that are inherent to the structure: for example, the order of elements within a parent element. These limitations are inherent in any attempt to infer general rules from a particular example document.

In general, therefore, you will need to iterate the process. You have a choice:

About DTDGen

DTDGen was written by Michael Kay of ICL.

It is supplied (and was written) as an demonstration of how to use the SAX interface to XML parsers. It has not been written or tested to production quality and you should not rely on it working.

I would like to know about bugs or enhancement suggestions but cannot guarantee to respond.

You may freely distribute DTDGen provided you include this description of the program as is. If you want to produce an improved version, please consult the author.

Michael H. Kay
29 April 1998