gpt4 book ai didi

java - Jaxb 应该编码内部类吗?

转载 作者:行者123 更新时间:2023-11-29 06:11:49 24 4
gpt4 key购买 nike

看,我有这 block 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 插件从模式生成了类,我得到了这个:

ResourcesTypeClasses上课, ClassRoomsTeachers内部类。在所有这些内部类中,我都保护了 List<Serializable>内容字段。它还生成了 ClassesType , ClassRoomsTypeTeachersType作为普通类(class)。

为什么会生成这个内部类?如果其他类不可序列化,我将如何设置此列表?

谢谢
最好的问候

最佳答案

Why was generated this inner classes ?

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>


How would I set this List if the other classes are not serializable ?

content 的允许内容属性(property)是JAXBElement<ResourceType>String . Serializable是这两种类型的通用接口(interface),比 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);
}

}

关于java - Jaxb 应该编码内部类吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6507626/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com