We have a DTD file and want to create the corresponding Java classes. Nobody wants to write the code by hand, therefore we create them by the use of the build in XML binding compiler xjc.

Java XML Binding Compiler

Below you can see the usage of the xjc command:

Usage: xjc [-options ...] <schema_file/URL/dir> ... [-b <bindinfo>] ...
 -nv                  : do not perform strict validation of the input schema(s)
 -extension           : allow vendor extensions - do not strictly follow the
                        Compatibility Rules and App E.2 from the JAXB Spec
 -b <file/dir>        : specify external bindings files (each <file> must have its own -b)
                        If a directory is given, **/*.xjb is searched
 -d <dir>             : generated files will go into this directory
 -p <pkg>             : specifies the target package
 -httpproxy <proxy>   : set HTTP/HTTPS proxy. Format is [user[:password]@]proxyHost:proxyPort
 -httpproxyfile <file>: set the proxy string (same format as above).
 -classpath <arg>     : specify where to find user class files
 -catalog <file>      : specify catalog files to resolve external entity references
                        support TR9401, XCatalog, and OASIS XML Catalog format.
 -readOnly            : generated files will be in read-only mode
 -npa                 : suppress generation of package level annotations (**/
 -no-header           : suppress generation of a file header with timestamp
 -target 2.0          : behave like XJC 2.0 and generate code that doesnt use any 2.1 features.
 -xmlschema           : treat input as W3C XML Schema (default)
 -relaxng             : treat input as RELAX NG (experimental,unsupported)
 -relaxng-compact     : treat input as RELAX NG compact syntax (experimental,unsupported)
 -dtd                 : treat input as XML DTD (experimental,unsupported)
 -wsdl                : treat input as WSDL and compile schemas inside it (experimental,unsupported)
 -verbose             : be extra verbose
 -quiet               : suppress compiler output
 -help                : display this help message
 -version             : display version information

Source Oracle

As you can see there is a command line parameter -dtd with which we can specify the input.

Example DTD

Lets take a DTD file like the one below and save it as example.dtd.

<!ELEMENT people_list (person)*>
<!ELEMENT person (name, birthdate?, gender?, socialsecuritynumber?)>
<!ELEMENT birthdate (#PCDATA)>
<!ELEMENT gender (#PCDATA)>
<!ELEMENT socialsecuritynumber (#PCDATA)>

Example DTD taken from wikipedia

Creating the Java classes from the DTD:

xjc -dtd -d targetDirectoy example.dtd

In our specific case it will look similar to this:

xjc -dtd -d /Users/myUser/Development/Java/DTDExample/ example.dtd
parsing a schema...
compiling a schema...
Laptop:DTDExample myUser$ cd generated
Laptop:generated myUser$ ls

Additionally you can also specify the package with -p your.package. This package will be automatically inserted into the Java classes.