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

前端 未结 2 1848
刺人心
刺人心 2020-12-17 03:36

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

The dtd looks like this:


    

        
相关标签:
2条回答
  • 2020-12-17 04:02
    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

    0 讨论(0)
  • 2020-12-17 04:13

    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 讨论(0)
提交回复
热议问题