8.2. Compiling DTD
The JAXB RI is shipped with an "experimental" DTD support, which let's you compile XML DTDs. It is marked "experimental" not because the feature is unstable nor unreliable, but rather because it's not a part of the JAXB specification and therefore the level of commitment to compatibility is lower.
To compile a DTD, run the XJC binding compiler as follows:
All the other command-line options of the XJC binding compiler can be applied. Similarly, the XJC ant task supports DTD. The generated code will be no different from what is generated from W3C XML Schema. You'll use the same JAXB API to access the generated code, and it is portable in the sense that it will run on any JAXB 2.0 implementation.
DTD long predates XML namespace, although people since then developed various techniques to use XML namespaces in conjunction with DTD. Because of this, XJC is currently unable to reverse-engineer the use of XML namespace from DTD. If you compile DTDs that use those techniques, you'd either manuallly modify the generated code, or you can try a tool like Trang that can convert DTD into XML Schema in ways that better preserves XML namespaces.
The customization syntax for DTD is roughly based on the ver.0.21 working draft of the JAXB specification, which is available at xml.coverpages.org. The deviations from this document are:
- The whitespace attribute of the conversion element takes "preserve", "replace", and "collapse" instead of "preserve", "normalize", and "collapse" as specified in the document.
- The interface customization just generates marker interfaces with no method.
8.2.2. Compiling DTD from Maven2
The following POM snippest describes how to invoke XJC to compile DTD from a Maven 2 project:
<!-- if you want to put DTD somewhere else
The dependencies section inside the plugin element can be used to specify additional XJC plugins. If you'd like to use more recent version of the JAXB RI, you can specify a dependency to XJC here to do so, like this:
The complete sample project is available from here.