包装一些生成的类, 我使用classImpl绑定,但生成的类中的集合返回生成的类型而不是classImpl中的类型,我想要一个classImpl列表...
我的xsd:
<complexType name="A">
<xs:sequence>
<element name="listB" type="sbs:B" minOccurs="0" maxOccurs="unbounded"></element>
<element name="singleB" type="sbs:B" minOccurs="1" maxOccurs="1"></element>
</xs:sequence>
</complexType>
<complexType name="B">
<xs:annotation><xs:appinfo>
<jxb:class implClass="BWrapper" />
</xs:appinfo></xs:annotation>
</complexType>
生成的类是:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "A", propOrder = {
"listB",
"singleB"
})
public class A {
@XmlElement(type = BWrapper.class)
protected List<B> listB;
@XmlElement(required = true, type = BWrapper.class)
protected BWrapper singleB;
正如预期的那样,singleB是键入的BWrapper,所以, 为什么listB是B的列表而不是BWrapper的列表???
提前感谢您的帮助!!
答案 0 :(得分:1)
您已定义BWrapper可以实现类型。您必须明确说明元素 listB应引用BWrapper。
我无法弄清楚如何在架构中设置此内联,因此我不得不使用外部.xjb文件。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<!-- bindings in the scope of the schema -->
<jaxb:bindings schemaLocation="./Test.xsd" node="/xs:schema">
<!-- apply bindings in the scope of the complex type B. -->
<jaxb:bindings node="//xs:complexType[@name='B']">
<!-- the java BWrapper extends the B object created by XJC -->
<jaxb:class implClass="com.foobar.BWrapper"/>
</jaxb:bindings>
<!-- specify bindings in the scope of the element 'listB' within -->
<!-- the the complex type A -->
<jaxb:bindings node="//xs:complexType[@name='A']//xs:element[@name='listB']">
<!-- the element should reference the BWrapper cLass -->
<jaxb:class ref="com.foobar.BWrapper"/>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
这将产生:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "A", propOrder = {
"listB",
"singleB"
})
public class A {
protected List<com.foobar.BWrapper> listB;
@XmlElement(required = true, type = com.foobar.BWrapper.class)
protected com.foobar.BWrapper singleB;
listB的getter返回BWrappers列表。我不确定为什么单个项目和列表之间存在这种不一致,但至少可以这样做。
答案 1 :(得分:0)
type
上的@XmlElement
属性是为此用例配置JAXB实现(Metro,MOXy,JaxMe等)的正确方法。如果您向班级添加XmlAccessorType
注释,是否仍会看到问题?
@XmlAccessorType(XmlAccessType.FIELD)
public class A {
@XmlElement(type = BWrapper.class)
protected List<B> listB;
@XmlElement(required = true, type = BWrapper.class)
protected BWrapper singleB;
}
有关示例,请参阅: