I want to generate Java classes from a dtd file using JAXB.
The dtd looks like this:
<!--Contents-->
<!ELEMENT persons (header, content) >
<!ELEMENT groups (header, content) >
<!--Header-->
<!ELEMENT header (version) >
<!ELEMENT version(#PCDATA) >
<!--Content-->
<!ELEMENT content(person, group)* >
<!--Person-->
<!ELEMENT person(p_id, p_name) >
<!ELEMENT p_id (#PCDATA) >
<!ELEMENT p_name (#PCDATA) >
<!--Group-->
<!ELEMENT group(g_id) >
<!ELEMENT g_id(#PCDATA) >
When generating the classes with JAXB I get the following ones:
- ObjectFactory
- Content
- Person
- Persons
- Group
- Groups
In the Content class the method to retreive all the persons and groups is
public List<Object> getPersonOrGroup() {
if (personOrGroup == null) {
personOrGroup = new ArrayList<Object>();
}
return this.personOrGroup;
}
Is there anything I can change in the dtd file so the generation of Java classes will make the persons and groups separated in the Content java class, so to retreive all persons and groups would be to make a call to Content.getPersons() and Content.getGroups() respectivly?
In his response, mavrav seems to tell that it’s impossible with DTD. I don’t know well how to use DTD.
But if you can, translate your DTD in XML schema.
I tried with this shema:
After I generated Java classes with the following cmd:
And it gives me the following code for the Content class: