JAXB没有为空的xs:element标签创建类

时间:2022-06-24 18:06:30

I am migrating a project from jaxb 1.0 to 2.1. When generating classes from an xsd using jaxb 2.1 (via an ant xjc task), the generated classes are missing elements which don't have any content like this:

我正在将项目从jaxb 1.0迁移到2.1。当使用jaxb 2.1(通过ant xjc任务)从xsd生成类时,生成的类缺少没有任何内容的元素:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="unqualified">
....
<xs:element name="get-all-services-partners-request"/>

However files are generated when the element does have content, like this:

但是,当元素具有内容时会生成文件,如下所示:

 <xs:element name="catalog-full-service-request">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="service-id" type="xs:string" minOccurs="0"/>
       <xs:element name="log-id" type="xs:string" minOccurs="0"/>
     </xs:sequence>
   </xs:complexType>
 </xs:element>

Why would this be? A previous set of generated classes (using jaxb 1.0) contain classes for all elements, not just those with content.

为什么会这样?前一组生成的类(使用jaxb 1.0)包含所有元素的类,而不仅仅是包含内容的类。

How do I get jaxb to generate these missing classes? I don't really have rights to change the xsd (but could do so if really necessary).

如何让jaxb生成这些缺失的类?我真的没有权利改变xsd(但如果真的有必要可以这样做)。

1 个解决方案

#1


5  

The good news is you don't need to generate the "missing classes", I'll demonstrate below with an example.

好消息是你不需要生成“缺失的类”,我将在下面举例说明。

schema.xsd

Below is a simplified version of your XML schema.

下面是XML架构的简化版本。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="unqualified">

    <xs:element name="get-all-services-partners-request" />

    <xs:complexType name="foo">
        <xs:sequence>
            <xs:element name="bar" type="xs:string" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

Foo

A JAXB (JSR-222) implementation will create a class per complex type (named or anonymous). In the above XML schema there is only one complex type (foo) so only on model class is generated.

JAXB(JSR-222)实现将为每个复杂类型(命名或匿名)创建一个类。在上面的XML模式中,只有一个复杂类型(foo),因此只生成模型类。

package forum12990635;

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo", propOrder = {
    "bar"
})
public class Foo {

    @XmlElement(required = true)
    protected String bar;

    public String getBar() {
        return bar;
    }

    public void setBar(String value) {
        this.bar = value;
    }

}

ObjectFactory

Global elements corresponding to named complex types corresond to @XmlElementDecl annotations on a class annotated with @XmlRegistry (see: http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html).

与命名复杂类型对应的全局元素与使用@XmlRegistry注释的类上的@XmlElementDecl注释相对应(请参阅:http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html)。

package forum12990635;

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 _GetAllServicesPartnersRequest_QNAME = new QName("", "get-all-services-partners-request");

    public ObjectFactory() {
    }

    public Foo createFoo() {
        return new Foo();
    }

    @XmlElementDecl(namespace = "", name = "get-all-services-partners-request")
    public JAXBElement<Object> createGetAllServicesPartnersRequest(Object value) {
        return new JAXBElement<Object>(_GetAllServicesPartnersRequest_QNAME, Object.class, null, value);
    }

}

Demo

Below is an example of how to create a document with the root element get-all-services-partners-request.

下面是如何使用根元素get-all-services-partners-request创建文档的示例。

package forum12990635;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance("forum12990635");

        Foo foo = new Foo();
        foo.setBar("Hello World");

        ObjectFactory objectFactory = new ObjectFactory();
        JAXBElement<Object> jaxbElement = objectFactory.createGetAllServicesPartnersRequest(foo);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(jaxbElement, System.out);
    }

}

Output

Below is the output from running the demo code.

以下是运行演示代码的输出。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<get-all-services-partners-request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="foo">
    <bar>Hello World</bar>
</get-all-services-partners-request>

#1


5  

The good news is you don't need to generate the "missing classes", I'll demonstrate below with an example.

好消息是你不需要生成“缺失的类”,我将在下面举例说明。

schema.xsd

Below is a simplified version of your XML schema.

下面是XML架构的简化版本。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="unqualified">

    <xs:element name="get-all-services-partners-request" />

    <xs:complexType name="foo">
        <xs:sequence>
            <xs:element name="bar" type="xs:string" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>

Foo

A JAXB (JSR-222) implementation will create a class per complex type (named or anonymous). In the above XML schema there is only one complex type (foo) so only on model class is generated.

JAXB(JSR-222)实现将为每个复杂类型(命名或匿名)创建一个类。在上面的XML模式中,只有一个复杂类型(foo),因此只生成模型类。

package forum12990635;

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

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo", propOrder = {
    "bar"
})
public class Foo {

    @XmlElement(required = true)
    protected String bar;

    public String getBar() {
        return bar;
    }

    public void setBar(String value) {
        this.bar = value;
    }

}

ObjectFactory

Global elements corresponding to named complex types corresond to @XmlElementDecl annotations on a class annotated with @XmlRegistry (see: http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html).

与命名复杂类型对应的全局元素与使用@XmlRegistry注释的类上的@XmlElementDecl注释相对应(请参阅:http://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html)。

package forum12990635;

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 _GetAllServicesPartnersRequest_QNAME = new QName("", "get-all-services-partners-request");

    public ObjectFactory() {
    }

    public Foo createFoo() {
        return new Foo();
    }

    @XmlElementDecl(namespace = "", name = "get-all-services-partners-request")
    public JAXBElement<Object> createGetAllServicesPartnersRequest(Object value) {
        return new JAXBElement<Object>(_GetAllServicesPartnersRequest_QNAME, Object.class, null, value);
    }

}

Demo

Below is an example of how to create a document with the root element get-all-services-partners-request.

下面是如何使用根元素get-all-services-partners-request创建文档的示例。

package forum12990635;

import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance("forum12990635");

        Foo foo = new Foo();
        foo.setBar("Hello World");

        ObjectFactory objectFactory = new ObjectFactory();
        JAXBElement<Object> jaxbElement = objectFactory.createGetAllServicesPartnersRequest(foo);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(jaxbElement, System.out);
    }

}

Output

Below is the output from running the demo code.

以下是运行演示代码的输出。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<get-all-services-partners-request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="foo">
    <bar>Hello World</bar>
</get-all-services-partners-request>