使用DTD文件中的JAXB生成Java类 - 如何修改DTD? [英] Generate Java classes with JAXB from a DTD file - how can I modify the DTD?

查看:112
本文介绍了使用DTD文件中的JAXB生成Java类 - 如何修改DTD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用JAXB从dtd文件生成Java类。

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

dtd如下所示:

<!--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) >

使用JAXB生成类时,我得到以下内容:

When generating the classes with JAXB I get the following ones:


  • ObjectFactory

  • 内容



  • 群组

  • 群组

  • 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;
}

我可以在dtd文件中更改任何内容,以便生成Java类将在内容 java中分隔类,所以要检索所有人和组将打电话给 Content.getPersons() Content.getGroups()分别?

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?

推荐答案

在他的回复中,mavrav似乎告诉DTD这是不可能的。我不太清楚如何使用DTD。
但是,如果可以,请在XML模式中翻译您的DTD。

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.

我试过这个shema:

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>

使用以下cmd生成Java类之后:

After I generated Java classes with the following cmd:

xjc -p com.mypackage schema.xsd

它为我提供了以下Content类的代码:

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;
    }
}

这篇关于使用DTD文件中的JAXB生成Java类 - 如何修改DTD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆