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
類包含與@XmlElementDecl
這些註釋2種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"/>
請不要使用像「沒有resut」這樣的短語。告訴我們它做了什麼,然後解釋你想做什麼。 – skaffman
maven插件應該爲每個元素生成單獨的類。 – Mirian
不,這不是真的,它會爲每種類型生成類。 – skaffman