Jaxb should Marshalling internal Classes?
Look I have this piece of XSD:
<xs:complexType name="ResourcesType">
<xs:sequence>
<xs:element name="Classrooms">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element type="ResourceType" name="Resource" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Teachers">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element type="ResourceType" name="Resource" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Classes"&开发者_C百科gt;
<xs:complexType mixed="true">
<xs:sequence>
<xs:element type="ResourceType" name="Resource" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="Special">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="0"/>
<xs:enumeration value=""/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
I generated the classes from the schema using the eclipse plugin and I got this:
ResourcesType
class with Classes
, ClassRooms
and Teachers
inner classes.
In all this inner classes I have protected List<Serializable>
content field.
It generated also ClassesType
, ClassRoomsType
and TeachersType
as normal classes.
Why was generated this inner classes ? How would I set this List if the other classes are not serializable ?
Thank you
Best regardsWhy was generated this inner classes ?
A JAXB implementation will inner classes for anonymous complex types. This is done in order to reduce the possibilities of name conflicts of generated classes.
<xs:complexType name="ResourcesType">
<xs:sequence>
<xs:element name="Classrooms">
<xs:complexType mixed="true">
<xs:sequence>
<xs:element type="ResourceType" name="Resource" maxOccurs="unbounded" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
...
</xs:sequence>
</xs:complexType>
How would I set this List if the other classes are not serializable ?
The allowed contents of the content
property are JAXBElement<ResourceType>
and String
. Serializable
is a common interface to both these types that is a little more restrictive than Object
.
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
public static class Classes {
@XmlElementRef(name = "Resource", type = JAXBElement.class)
@XmlMixed
protected List<Serializable> content;
/**
* Gets the value of the content property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the content property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getContent().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link JAXBElement }{@code <}{@link ResourceType }{@code >}
* {@link String }
*
*
*/
public List<Serializable> getContent() {
if (content == null) {
content = new ArrayList<Serializable>();
}
return this.content;
}
}
UPDATE
The following example may help:
package example;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import example.ResourcesType.Classrooms;
public class Demo {
public static void main(String[] args) throws Exception {
// This is the ObjectFactory that was generated by XJC
ObjectFactory objectFactory = new ObjectFactory();
// You can instantiate objects using the constructors
ResourcesType resourcesType = new ResourcesType();
// You can instantiate objects using the ObjectFactory
Classrooms classRooms = objectFactory.createResourcesTypeClassrooms();
resourcesType.setClassrooms(classRooms);
// You can use the ObjectFactory to wrap an object in a JAXBElement
ResourceType resourceType1 = new ResourceType();
JAXBElement<ResourceType> jaxbElement1 = objectFactory.createResourcesTypeClassesResource(resourceType1);
classRooms.getContent().add(jaxbElement1);
ResourceType resourceType2 = objectFactory.createResourceType();
JAXBElement<ResourceType> jaxbElement2 = objectFactory.createResourcesTypeClassesResource(resourceType2);
classRooms.getContent().add(jaxbElement2);
// You can create a JAXBContext on the package name of your generated classes
JAXBContext jc = JAXBContext.newInstance("example");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// You can also create an instance of JAXBElement without using the ObjectFactory
JAXBElement<ResourcesType> rootElement = new JAXBElement<ResourcesType>(new QName("root"), ResourcesType.class, resourcesType);
marshaller.marshal(rootElement, System.out);
}
}
精彩评论