3

为了包装一些生成的类,我使用 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 的列表?

在此先感谢您的帮助 !!

4

2 回答 2

2

您已经定义了该类型 可以由 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 列表。我不确定为什么单个项目和列表之间存在这种不一致,但至少这是可行的。

于 2016-01-13T05:09:44.867 回答
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;
}

有关示例,请参见:

于 2011-06-16T14:12:42.050 回答