jaxb不会为同一类型的不同元素生成类

时间:2012-01-20 12:15:44

标签: xml jaxb

xsd架构包含两个具有相同类型的不同元素:

<element name="subscriber" type="ns1:CreateSubscriberType"/>
<element name="systemSubscriber" type="ns1:CreateSubscriberType"/>

<complexType name="CreateSubscriberType">
<annotation>
  <documentation>bla bla bla</documentation>
</annotation>
<sequence>
  <element name="Header" type="ns2:DocumentHeader"/>
  <element name="SubscriberDefault" type="ns2:SubscriberDefaultType">
    <annotation>
      <documentation>bla bla</documentation>
    </annotation>
  </element>
</sequence>
</complexType>

xsd架构包含两个具有相同类型的不同元素: 然后我尝试使用maven-jaxb2-plugin从这个xsd生成类,并且没有resut。这些类没有生成。如果我调用其中一个元素的类型,它将正常工作,并将生成2个类。我在官方文档中没有找到解释。任何人都可以遇到这样的问题以及如何解决这个问题

3 个答案:

答案 0 :(得分:3)

JAXB(JSR-222)实现将为每个复杂类型生成一个类。这很好,因为可以在与该类型的属性/元素对应的任何字段/属性上设置此类的实例。对于命名的复杂类型,引用它们的全局元素将作为ObjectFactory类中的元数据捕获。

<强> schema.xsd

以下是XML架构的略微简化版本:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org"
    xmlns:ns1="http://www.example.org" elementFormDefault="qualified">

    <element name="subscriber" type="ns1:CreateSubscriberType" />
    <element name="systemSubscriber" type="ns1:CreateSubscriberType" />

    <complexType name="CreateSubscriberType">
        <annotation>
            <documentation>bla bla bla</documentation>
        </annotation>
        <sequence/>
    </complexType>

</schema>

XJC致电

xjc -d out -p forum8941337 schema.xsd

<强> CreateSubscriberType

以下是为复杂类型生成的类:

package forum8941337;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "CreateSubscriberType")
public class CreateSubscriberType {

}

<强>的ObjectFactory

生成的ObjectFactory类包含两个用create注释的@XmlElementDecl方法,这些方法对应于XML架构中的两个全局元素。

package forum8941337;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    private final static QName _Subscriber_QNAME = new QName("http://www.example.org", "subscriber");
    private final static QName _SystemSubscriber_QNAME = new QName("http://www.example.org", "systemSubscriber");

    public ObjectFactory() {
    }

    public CreateSubscriberType createCreateSubscriberType() {
        return new CreateSubscriberType();
    }

    @XmlElementDecl(namespace = "http://www.example.org", name = "subscriber")
    public JAXBElement<CreateSubscriberType> createSubscriber(CreateSubscriberType value) {
        return new JAXBElement<CreateSubscriberType>(_Subscriber_QNAME, CreateSubscriberType.class, null, value);
    }

    @XmlElementDecl(namespace = "http://www.example.org", name = "systemSubscriber")
    public JAXBElement<CreateSubscriberType> createSystemSubscriber(CreateSubscriberType value) {
        return new JAXBElement<CreateSubscriberType>(_SystemSubscriber_QNAME, CreateSubscriberType.class, null, value);
    }

}

<强>演示

package forum8941337;

import javax.xml.bind.*;
public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance("forum8941337");
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        CreateSubscriberType subscriberType = new CreateSubscriberType();
        ObjectFactory objectFactory = new ObjectFactory();

            // System Subscriber
        JAXBElement<CreateSubscriberType> systemSubscriber = objectFactory.createSystemSubscriber(subscriberType);
        marshaller.marshal(systemSubscriber, System.out);

            // Subscriber
        JAXBElement<CreateSubscriberType> subscriber = objectFactory.createSubscriber(subscriberType);
        marshaller.marshal(subscriber, System.out);
    }

}

<强>输出

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<systemSubscriber xmlns="http://www.example.org"/>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<subscriber xmlns="http://www.example.org"/>

答案 1 :(得分:0)

经过深入研究后,我们发现当从xs:dateTime到joda.DateTime添加外部自定义绑定时会生成全局元素。我在xjc编译器上测试过它: xjc -extension -b custom-binding.xjb outSchema.xsd。 对JAXB 2.1.10不是正确的行为吗?

答案 2 :(得分:0)

我通过jaxb绑定来解决它:

<jaxb:bindings schemaLocation="youra.xsd" node="/xs:schema"> 
    <jaxb:bindings node="//xs:element[@name = 'subscriber']">
        <jaxb:class name="SubscriberClass" />
    </jaxb:bindings>
    <jaxb:bindings node="//xs:element[@name = 'systemSubscriber']">
        <jaxb:class name="SystemSubscriberClass" />
    </jaxb:bindings>
</jaxb:bindings>