JAXB does not create member variables and recipients and setters

I am trying to generate Java form classes from an xsd schema and I am using JAXB. For the most part, when I run the process to create classes, it works. However, there are several classes that do not generate a member variable, getters, and setters. That's what i

Ns2.xsd file

<xs:element name="Observation" type="ns2:ObservationType" substitutionGroup="ns1:_MetaData"/> <xs:complexType name="ObservationType" mixed="true"> <xs:complexContent mixed="true"> <xs:extension base="ns1:AbstractType"> <xs:sequence> <xs:element ref="ns2:identifier"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> 

Ns3.xsd file

 <xs:element name="Observation" type="ns3:ObservationType" substitutionGroup="ns2:Observation"/> <xs:complexType name="ObservationType" mixed="true"> <xs:annotation> <xs:documentation>this extends the ns2:ObservationType </xs:documentation> </xs:annotation> <xs:complexContent mixed="true"> <xs:extension base="ns2:ObservationType"> <xs:sequence> <xs:element ref="ns3:deliveryInfo" minOccurs="0"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> 

This creates an empty class.

 package mypackage.ns3; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlType; /** * this extends the ns2:ObservationType * * <p>Java class for ObservationType complex type. * * <p>The following schema fragment specifies the expected content contained within this class. * * <pre> * &lt;complexType name="ObservationType"> * &lt;complexContent> * &lt;extension base="{http://earth.esa.int/ns2}ObservationType"> * &lt;sequence> * &lt;element ref="{http://earth.esa.int/ns3}deliveryInfo" minOccurs="0"/> * &lt;/sequence> * &lt;/extension> * &lt;/complexContent> * &lt;/complexType> * </pre> * * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "ObservationType") public class ObservationType extends mypackage.ns2.ObservationType { } 

My question is: why is the required member variable and its setters and getters not created? Is there something wrong with the schema, or is there a JAXB restriction to create opaque forms of information forms that use extensions from different files? Thanks in advance. Your help or comments will be appreciated.

+4
source share
1 answer

What makes this use case odd is that you have inheritance in your XML schema between two types with mixed content. I think there is an XJC error (and possibly spec) here, and as suggested by Puce, you should enter the error for it at the following link:

XML Schema

schema.xsd

Here is a simpler XML schema that reproduces the same problem:

 <?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/schema" xmlns:tns="http://www.example.org/schema" elementFormDefault="qualified"> <complexType name="b" mixed="true"> <sequence> <element ref="tns:bb"/> </sequence> </complexType> <complexType name="c"> <complexContent mixed="true"> <extension base="tns:b"> <sequence> <element ref="tns:cc"/> </sequence> </extension> </complexContent> </complexType> <element name="bb" type="string"/> <element name="cc" type="string"/> </schema> 

Invalid Java Model

IN

The class generated for type b is good.

 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "b", propOrder = {"content"}) @XmlSeeAlso({C.class}) public class B { @XmlElementRef(name = "bb", namespace = "http://www.example.org/schema", type = JAXBElement.class) @XmlMixed protected List<Serializable> content; public List<Serializable> getContent() { if (content == null) { content = new ArrayList<Serializable>(); } return this.content; } } 

FROM

The class generated for type c is incorrect, the question is, what should it be?

 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "c") public class C extends B { } 

Improved Java Model? - Option number 1

You can add property to class c . Then the question arises as to which of the mixed text belongs to a property inherited from b and which is part of the property defined in c .

 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "c") public class C extends B { @XmlElementRef(name = "cc", namespace = "http://www.example.org/schema", type = JAXBElement.class) @XmlMixed protected List<Serializable> content2; } 

Improved Java Model? - Option number 2

You can extend the property to b to know the link to an element from type c . This will allow XML to be processed correctly for types b and c , but will allow some documents that were not valid with respect to the XML schema.

 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "b", propOrder = {"content"}) @XmlSeeAlso({C.class}) public class B { @XmlElementRefs({ @XmlElementRef(name = "bb", namespace = "http://www.example.org/schema", type = JAXBElement.class), @XmlElementRef(name = "cc", namespace = "http://www.example.org/schema", type = JAXBElement.class) }) @XmlMixed protected List<Serializable> content; public List<Serializable> getContent() { if (content == null) { content = new ArrayList<Serializable>(); } return this.content; } } 
+2
source

Source: https://habr.com/ru/post/1494566/


All Articles