JAXB不生成所有元素类(JAXB doesn't generate all element classes)

编程入门 行业动态 更新时间:2024-10-24 19:28:19
JAXB不生成所有元素类(JAXB doesn't generate all element classes)

对于REST服务,我定义了XML中必需的DTO以生成它的JAXB对象。 对于第一代,我使用Eclipse IDE的内置选项。

问题是类不会按预期生成。 鉴于下面的XML Schema文件,我期待3分类生成。 ImageType , Image和Images 。 但Image元素的Image类不会生成。 目前我不知道我做错了什么。

<?xml version="1.0" encoding="UTF-8"?> <schema targetNamespace="http://www.company.com/schema/v1/ImageDTO" elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:Q1="http://www.company.com/schema/v1/BusinessObjectDTO" xmlns:Q2="http://www.company.com/schema/v1/ImageDTO"> <import schemaLocation="BusinessObjectDTO.xsd" namespace="http://www.company.com/schema/v1/BusinessObjectDTO"></import> <complexType name="ImageType" abstract="false"> <complexContent> <extension base="Q1:BusinessObjectType"> <sequence> <element name="name" type="string" maxOccurs="1" minOccurs="1"></element> <element name="fileName" type="string" maxOccurs="1" minOccurs="1"></element> <element name="thumbnailFileName" type="string" maxOccurs="1" minOccurs="1"></element> </sequence> </extension> </complexContent> </complexType> <element name="Image" type="Q2:ImageType"></element> <element name="Images"> <complexType> <sequence> <element name="ImageList" type="Q2:ImageType" maxOccurs="unbounded" minOccurs="0"></element> </sequence> </complexType> </element> </schema>

For a REST service I define the necessary DTOs in XML to generate JAXB object of it. For generation I use the built in option of the Eclipse IDE.

The problem is that the classes don't get generated as expected. Given the XML Schema file below I'm expecting 3 classed to be generated. ImageType, Image and Images. But the Image class for the Image element doesn't get generated. At the moment I don't know what I'm doing wrong.

<?xml version="1.0" encoding="UTF-8"?> <schema targetNamespace="http://www.company.com/schema/v1/ImageDTO" elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema" xmlns:Q1="http://www.company.com/schema/v1/BusinessObjectDTO" xmlns:Q2="http://www.company.com/schema/v1/ImageDTO"> <import schemaLocation="BusinessObjectDTO.xsd" namespace="http://www.company.com/schema/v1/BusinessObjectDTO"></import> <complexType name="ImageType" abstract="false"> <complexContent> <extension base="Q1:BusinessObjectType"> <sequence> <element name="name" type="string" maxOccurs="1" minOccurs="1"></element> <element name="fileName" type="string" maxOccurs="1" minOccurs="1"></element> <element name="thumbnailFileName" type="string" maxOccurs="1" minOccurs="1"></element> </sequence> </extension> </complexContent> </complexType> <element name="Image" type="Q2:ImageType"></element> <element name="Images"> <complexType> <sequence> <element name="ImageList" type="Q2:ImageType" maxOccurs="unbounded" minOccurs="0"></element> </sequence> </complexType> </element> </schema>

最满意答案

您已将Image定义为ImageType类型的元素。 因此Image只是一个与该类型一起使用的名称。 ImageType定义将转换为Java类,并且当在模式中的某处引用Image元素时,会导致ImageType类型的字段注释为名为Image的XML元素。

所以说你有...

<element ref="Image" minOccurs="1" maxOccurs="1" />

在某个类型定义中的某处,会导致...

@XmlElement(name="Image" ...) ImageType image;

...在相应的班级中。

Images确实获得类定义的原因是因为您已将其定义为complexType inline。 Image是指一种类型,所以他们只是使用相应的类。 Images具有匿名类型定义,因此必须生成一个类以捕获其结构。

You've defined Image as an element of type ImageType. Image is thus just a name used with that type. The ImageType definition will be turned into a Java class, and when the Image element is refered somewhere in your schema, that'd result in a field of type ImageType annotated as being an XML element with the name Image.

So say that you have...

<element ref="Image" minOccurs="1" maxOccurs="1" />

somewhere in a type definition, that'd result in ...

@XmlElement(name="Image" ...) ImageType image;

... in the corresponding class.

The reason why Images did get a class definition is because you've defined it as a complexType inline. Image refers to a type, so they're just using the corresponding class. Images has an anonymous type definition, so a class must be generated to capture its structure.

更多推荐

XML,生成,ImageType,电脑培训,计算机培训,IT培训"/> <meta name="description&

本文发布于:2023-08-07 01:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1458556.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   JAXB   generate   classes   element

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!