使用 JAXB 从 DTD 文件生成 Java 类 - 如何修改 DTD?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10312159/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate Java classes with JAXB from a DTD file - how can I modify the DTD?
提问by Rox
I want to generate Java classes from a dtd file using JAXB.
我想使用 JAXB 从 dtd 文件生成 Java 类。
The dtd looks like this:
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) >
When generating the classes with JAXB I get the following ones:
使用 JAXB 生成类时,我得到以下内容:
- ObjectFactory
- Content
- Person
- Persons
- Group
- Groups
- 对象工厂
- 内容
- 人
- 人
- 团体
- 团体
In the Content class the method to retreive all the persons and groups is
在 Content 类中,检索所有人员和组的方法是
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?
我可以在 dtd 文件中更改任何内容,以便 Java 类的生成将使 Java 类中的persons
和groups
分开Content
,因此要检索所有人员和组将分别调用Content.getPersons()
和Content.getGroups()
?
采纳答案by Pith
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.
在他的回应中,mavrav 似乎在说 DTD 是不可能的。我不太清楚如何使用 DTD。但是,如果可以,请将 DTD 转换为 XML 模式。
I tried with this shema:
我试过这个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:
在我使用以下 cmd 生成 Java 类之后:
xjc -p com.mypackage schema.xsd
And it gives me the following code for the Content class:
它为我提供了 Content 类的以下代码:
@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;
}
}
回答by surya
xjc -dtd -d generatedsrc -p com.examples log4j.dtd
will generate the classes in directory generatedsrc and the package used will be com.examples.
将在生成的目录中生成类,使用的包将是 com.examples。
you can find more information here: http://www.javaworld.com/community/node/7622
您可以在此处找到更多信息:http: //www.javaworld.com/community/node/7622