Until I read the following blog post, JAXB xjc Java Generation with DTD, I was also thinking that there was no binding solution for DTDs when you try to map XML content into Java objects.
For that reason, while maintaining some legacy applications using DTD, I was being forced to write my own parser(using SAX generally) for each different XML structure.
Luckily, although not part of the JAXB specification, the XJC tool offers the ability to generate Java classes from a DTD.
Simply execute the following command in your console,
"%JAVA_HOME%\bin\xjc" -dtd -d generatedsrc -p the.target.packagename sample.dtd
Sample DTD File : Product Catalog
<!ENTITY AUTHOR "John Doe"> <!ENTITY COMPANY "JD Power Tools, Inc."> <!ENTITY EMAIL "jd@jd-tools.com"> <!ELEMENT CATALOG (PRODUCT+)> <!ELEMENT PRODUCT (SPECIFICATIONS+,OPTIONS?,PRICE+,NOTES?)> <!ATTLIST PRODUCT NAME CDATA #IMPLIED CATEGORY (HandTool|Table|Shop-Professional) "HandTool" PARTNUM CDATA #IMPLIED PLANT (Pittsburgh|Milwaukee|Chicago) "Chicago" INVENTORY (InStock|Backordered|Discontinued) "InStock"> <!ELEMENT SPECIFICATIONS (#PCDATA)> <!ATTLIST SPECIFICATIONS WEIGHT CDATA #IMPLIED POWER CDATA #IMPLIED> <!ELEMENT OPTIONS (#PCDATA)> <!ATTLIST OPTIONS FINISH (Metal|Polished|Matte) "Matte" ADAPTER (Included|Optional|NotApplicable) "Included" CASE (HardShell|Soft|NotApplicable) "HardShell"> <!ELEMENT PRICE (#PCDATA)> <!ATTLIST PRICE MSRP CDATA #IMPLIED WHOLESALE CDATA #IMPLIED STREET CDATA #IMPLIED SHIPPING CDATA #IMPLIED> <!ELEMENT NOTES (#PCDATA)>Running XJC and producing Java classes
xjc.exe -dtd -d generatedsrc -p com.example.product.catalog product_catalog.dtd parsing a schema... compiling a schema... com\example\product\catalog\CATALOG.java com\example\product\catalog\OPTIONS.java com\example\product\catalog\ObjectFactory.java com\example\product\catalog\PRICE.java com\example\product\catalog\PRODUCT.java com\example\product\catalog\SPECIFICATIONS.javaProduct.java