Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate Java classes with JAXB from a DTD file - how can I modify the DTD?

Tags:

java

xml

jaxb

dtd

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?

like image 380
Rox Avatar asked Apr 25 '12 08:04

Rox


People also ask

How do you auto generate JAXB classes?

Open a command prompt. Run the JAXB schema compiler, xjc command from the directory where the schema file is located. The xjc schema compiler tool is located in the app_server_root \bin\ directory. Use the generated JAXB objects within a Java application to manipulate XML content through the generated JAXB classes.

How do I generate Java classes from XSD using JAXB in Intellij?

Generate a Java class from an XML Schema using JAXB In the active editor tab, open the desired Schema . xsd file or an XML document, which contains the desired Schema. In the main menu, go to Tools | XML Actions | Generate Java Code From XML Schema Using JAXB.

How do I generate Java classes from XSD using JAXB maven?

Just build the maven project using mvn clean install and you will see java classes generated in target/generated-sources/jaxb directory.

What is JAXB Java classes?

The JAXB compiler generates Java classes that map to constraints in the source XML schema. The classes implements get and set methods that you can use to obtain and specify data for each type of element and attribute in the schema. Process XML documents by instantiating the generated classes in a Java program.


2 Answers

xjc -dtd -d generatedsrc -p com.examples log4j.dtd

will generate the classes in directory generatedsrc and the package used will be com.examples.

you can find more information here: http://www.javaworld.com/community/node/7622

like image 126
surya Avatar answered Nov 15 '22 07:11

surya


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:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:hr="http://mycompany.com/schema"
        elementFormDefault="qualified"
        targetNamespace="http://mycompany.com/schema">
    <!-- Contents -->
    <xs:element name="persons">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="header" />
                <xs:element name="content" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="groups">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="header" />
                <xs:element name="content" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <!-- Header -->
    <xs:element name="header">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="version" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <!-- Content -->
    <xs:element name="content">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="person" maxOccurs="unbounded" />
                <xs:element name="group" maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <!-- Person -->
    <xs:element name="person">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="p_id" type="xs:integer" />
                <xs:element name="p_name" type="xs:string" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <!-- Group -->
    <xs:element name="group">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="g_id" type="xs:integer" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

After I generated Java classes with the following cmd:

xjc -p com.mypackage schema.xsd

And it gives me the following code for the Content class:

@XmlRootElement(name = "content")
public class Content {

    @XmlElement(required = true)
    protected List<Object> person;
    @XmlElement(required = true)
    protected List<Object> group;

    public List<Object> getPerson() {
        if (person == null) {
            person = new ArrayList<Object>();
        }
        return this.person;
    }

    public List<Object> getGroup() {
        if (group == null) {
            group = new ArrayList<Object>();
        }
        return this.group;
    }
}
like image 43
Pith Avatar answered Nov 15 '22 06:11

Pith