爲什麼產生這個內部類?
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;
}
}
UPDATE
下面的例子可能會有幫助:
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);
}
}
是的,但是我會如何的對象添加到這個列表?我應該在我的類型中實現可序列化還是將列表更改爲我的ResourceType? – Felipe
@ user819285 - 在將它們添加到List之前,您需要將'ResourceType'的實例包裝在'JAXBElement'的實例中。 –
怎麼樣?請舉例嗎?我是新手對不起。 – Felipe