Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7840303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:44:07+00:00 2026-06-02T15:44:07+00:00

I want to generate Java classes from a dtd file using JAXB. The dtd

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:44:09+00:00Added an answer on June 2, 2026 at 3:44 pm

    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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm want to generate java classes from a schema using jaxb, but I am
I want to generate java code from xsd using JAXB 2.1 XJC. I have
I am using xjc to generate Java classes from the XML schema and the
I am using Eclipse and JBoss Tools to generate Java classes from existing Hibernate
Our project uses XJC to generate Java classes from an XSD. I'm using JAVA
I am using jax-ws's wsimport to generate java classes from WSDLs. I am using
I want to generate Javascript code from an existing Java project ( original question
I'm trying to use JAXB to unmarshal this file into Java objects. I know
Apache XMLBeans can be used to generate Java classes and interfaces from XML Schema
I want to autogenerate some java classes from interfaces. My first thought was to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.