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,
1 | "%JAVA_HOME%\bin\xjc" -dtd -d generatedsrc -p the.target.packagename sample.dtd |
1 2 3 | -d <dir> : generated files will go into this directory -p <pkg> : specifies the target package -dtd : treat input as XML DTD |
Sample DTD File : Product Catalog
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | <!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)> |
1 2 3 4 5 6 7 8 9 | 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.java |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | // // This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 // See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> // Any modifications to this file will be lost upon recompilation of the source schema. // Generated on: 2013.11.02 at 02:15:25 PM VET // package com.example.product.catalog; import java.util.ArrayList; import java.util.List; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; import javax.xml.bind.annotation.adapters.NormalizedStringAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; /** * */ @XmlAccessorType (XmlAccessType.FIELD) @XmlType (name = "" , propOrder = { "specifications" , "options" , "price" , "notes" }) @XmlRootElement (name = "PRODUCT" ) public class PRODUCT { @XmlAttribute (name = "NAME" ) @XmlJavaTypeAdapter (NormalizedStringAdapter. class ) protected String name; @XmlAttribute (name = "CATEGORY" ) @XmlJavaTypeAdapter (CollapsedStringAdapter. class ) protected String category; @XmlAttribute (name = "PARTNUM" ) @XmlJavaTypeAdapter (NormalizedStringAdapter. class ) protected String partnum; @XmlAttribute (name = "PLANT" ) @XmlJavaTypeAdapter (CollapsedStringAdapter. class ) protected String plant; @XmlAttribute (name = "INVENTORY" ) @XmlJavaTypeAdapter (CollapsedStringAdapter. class ) protected String inventory; @XmlElement (name = "SPECIFICATIONS" , required = true ) protected List<SPECIFICATIONS> specifications; @XmlElement (name = "OPTIONS" ) protected OPTIONS options; @XmlElement (name = "PRICE" , required = true ) protected List<PRICE> price; @XmlElement (name = "NOTES" ) protected String notes; /** * Gets the value of the name property. * * @return * possible object is * {@link String } * */ public String getNAME() { return name; } /** * Sets the value of the name property. * * @param value * allowed object is * {@link String } * */ public void setNAME(String value) { this .name = value; } /** * Gets the value of the category property. * * @return * possible object is * {@link String } * */ public String getCATEGORY() { if (category == null ) { return "HandTool" ; } else { return category; } } /** * Sets the value of the category property. * * @param value * allowed object is * {@link String } * */ public void setCATEGORY(String value) { this .category = value; } /** * Gets the value of the partnum property. * * @return * possible object is * {@link String } * */ public String getPARTNUM() { return partnum; } /** * Sets the value of the partnum property. * * @param value * allowed object is * {@link String } * */ public void setPARTNUM(String value) { this .partnum = value; } /** * Gets the value of the plant property. * * @return * possible object is * {@link String } * */ public String getPLANT() { if (plant == null ) { return "Chicago" ; } else { return plant; } } /** * Sets the value of the plant property. * * @param value * allowed object is * {@link String } * */ public void setPLANT(String value) { this .plant = value; } /** * Gets the value of the inventory property. * * @return * possible object is * {@link String } * */ public String getINVENTORY() { if (inventory == null ) { return "InStock" ; } else { return inventory; } } /** * Sets the value of the inventory property. * * @param value * allowed object is * {@link String } * */ public void setINVENTORY(String value) { this .inventory = value; } /** * Gets the value of the specifications property. * * <p> * This accessor method returns a reference to the live list, * not a snapshot. Therefore any modification you make to the * returned list will be present inside the JAXB object. * This is why there is not a <CODE>set</CODE> method for the specifications property. * * <p> * For example, to add a new item, do as follows: * <pre> * getSPECIFICATIONS().add(newItem); * </pre> * * * <p> * Objects of the following type(s) are allowed in the list * {@link SPECIFICATIONS } * * */ public List<SPECIFICATIONS> getSPECIFICATIONS() { if (specifications == null ) { specifications = new ArrayList<SPECIFICATIONS>(); } return this .specifications; } /** * Gets the value of the options property. * * @return * possible object is * {@link OPTIONS } * */ public OPTIONS getOPTIONS() { return options; } /** * Sets the value of the options property. * * @param value * allowed object is * {@link OPTIONS } * */ public void setOPTIONS(OPTIONS value) { this .options = value; } /** * Gets the value of the price property. * * <p> * This accessor method returns a reference to the live list, * not a snapshot. Therefore any modification you make to the * returned list will be present inside the JAXB object. * This is why there is not a <CODE>set</CODE> method for the price property. * * <p> * For example, to add a new item, do as follows: * <pre> * getPRICE().add(newItem); * </pre> * * * <p> * Objects of the following type(s) are allowed in the list * {@link PRICE } * * */ public List<PRICE> getPRICE() { if (price == null ) { price = new ArrayList<PRICE>(); } return this .price; } /** * Gets the value of the notes property. * * @return * possible object is * {@link String } * */ public String getNOTES() { return notes; } /** * Sets the value of the notes property. * * @param value * allowed object is * {@link String } * */ public void setNOTES(String value) { this .notes = value; } } |