Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

xml - Generate Java classes with JAXB from a DTD file - how can I modify the 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...