Реализация 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 Call
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"/>