xml - jaxb 不会为相同类型的不同元素生成类

标签 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 生成类,但没有结果。没有生成类。如果我更改其中一个元素的类型,它将正常工作并生成 2 个类。我没有在官方文档中找到解释。任何人都可以遇到这样的问题以及如何解决它

最佳答案

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 -d out -p forum8941337 schema.xsd

创建订阅者类型

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

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 类包含两个用 @XmlElementDecl 注释的 create 方法,它们对应于 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"/>

关于xml - jaxb 不会为相同类型的不同元素生成类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8941337/

相关文章:

Java Web 服务客户端 cdata 标签

xml - RDF有效字符

java - JaxB 自动从 XML 解析为 Java 类

java - 如果未设置命名空间前缀,则解码对象为 null

java - 在 Android 上保护 XML

java - 使用 JAXB 解析响应 XML

java - 编码 jaxb 时从根元素中删除 xmlns 属性

python - 如何通过搜索元素的属性在 Python 中使用 ElementTree 删除 xml 中的节点?

使用 XSLT 将 XML 转换为 CSV

java - EclipseLink MOXy 是否适合大得离谱的 XML 文件?