看,我有这片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">
<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>
我使用eclipse插件从模式生成了类,我得到了这个:
带有ResourcesType
,Classes
和ClassRooms
内部课程的 Teachers
课程。
在所有这些内部类中,我保护了List<Serializable>
内容字段。
它还生成ClassesType
,ClassRoomsType
和TeachersType
作为普通类。
为什么生成这个内部类?如果其他类不可序列化,我将如何设置此List?
谢谢你 最好的问候
答案 0 :(得分:3)
为什么生成了这个内部类?
JAXB实现将是匿名复杂类型的内部类。这样做是为了减少生成的类的名称冲突的可能性。
<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>
<小时/>
如果另一个我如何设置此列表 类不可序列化?
content
属性的允许内容为JAXBElement<ResourceType>
和String
。 Serializable
是这两种类型的通用接口,比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;
}
}
<强>更新强>
以下示例可能有所帮助:
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);
}
}