以下は役立つ例です。
XML スキーマ - schema.xsd
と の両方でJAXBElement<Boolean>
ある要素がある場合、 type のプロパティを取得します。nillable="true"
minOccurs="0"
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/schema"
xmlns:tns="http://www.example.org/schema"
elementFormDefault="qualified">
<element name="root">
<complexType>
<sequence>
<element name="trueValue" type="boolean" minOccurs="0" nillable="true"/>
<element name="falseValue" type="boolean" minOccurs="0" nillable="true"/>
<element name="nullValue" type="boolean" minOccurs="0" nillable="true"/>
<element name="notSetValue" type="boolean" minOccurs="0" nillable="true"/>
</sequence>
</complexType>
</element>
</schema>
生成されたモデル - ルート
以下は、要素から生成されたクラスがどのroot
ように見えるかです:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"trueValue", "falseValue", "nullValue", "notSetValue"})
@XmlRootElement(name = "root")
public class Root {
@XmlElementRef(name = "trueValue", namespace = "http://www.example.org/schema", type = JAXBElement.class, required = false)
protected JAXBElement<Boolean> trueValue;
@XmlElementRef(name = "falseValue", namespace = "http://www.example.org/schema", type = JAXBElement.class, required = false)
protected JAXBElement<Boolean> falseValue;
@XmlElementRef(name = "nullValue", namespace = "http://www.example.org/schema", type = JAXBElement.class, required = false)
protected JAXBElement<Boolean> nullValue;
@XmlElementRef(name = "notSetValue", namespace = "http://www.example.org/schema", type = JAXBElement.class, required = false)
protected JAXBElement<Boolean> notSetValue;
}
生成されたモデル - ObjectFactory
@XmlRegistry
public class ObjectFactory {
private final static QName _RootNullValue_QNAME = new QName("http://www.example.org/schema", "nullValue");
private final static QName _RootTrueValue_QNAME = new QName("http://www.example.org/schema", "trueValue");
private final static QName _RootFalseValue_QNAME = new QName("http://www.example.org/schema", "falseValue");
private final static QName _RootNotSetValue_QNAME = new QName("http://www.example.org/schema", "notSetValue");
public ObjectFactory() {
}
public Root createRoot() {
return new Root();
}
@XmlElementDecl(namespace = "http://www.example.org/schema", name = "nullValue", scope = Root.class)
public JAXBElement<Boolean> createRootNullValue(Boolean value) {
return new JAXBElement<Boolean>(_RootNullValue_QNAME, Boolean.class, Root.class, value);
}
@XmlElementDecl(namespace = "http://www.example.org/schema", name = "trueValue", scope = Root.class)
public JAXBElement<Boolean> createRootTrueValue(Boolean value) {
return new JAXBElement<Boolean>(_RootTrueValue_QNAME, Boolean.class, Root.class, value);
}
@XmlElementDecl(namespace = "http://www.example.org/schema", name = "falseValue", scope = Root.class)
public JAXBElement<Boolean> createRootFalseValue(Boolean value) {
return new JAXBElement<Boolean>(_RootFalseValue_QNAME, Boolean.class, Root.class, value);
}
@XmlElementDecl(namespace = "http://www.example.org/schema", name = "notSetValue", scope = Root.class)
public JAXBElement<Boolean> createRootNotSetValue(Boolean value) {
return new JAXBElement<Boolean>(_RootNotSetValue_QNAME, Boolean.class, Root.class, value);
}
}
デモ
JAXBElement<Boolean>
以下は、4 つの可能なオプションのそれぞれを示すために、各プロパティを異なる方法で設定するデモ コードです。
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("forum15170433");
Root root = new Root();
ObjectFactory objectFactory = new ObjectFactory();
// set the value to true
root.setTrueValue(objectFactory.createRootTrueValue(true));
// set the value to false
root.setFalseValue(objectFactory.createRootFalseValue(false));
// set the value to null
root.setNullValue(objectFactory.createRootNullValue(null));
// specify the value is unset
root.setNotSetValue(null);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
出力
以下は、デモ コードを実行した結果の出力です。xsi:nil
属性が値を表すために使用されnull
、設定されていない値がマーシャリングされていないことがわかります。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root xmlns="http://www.example.org/schema">
<trueValue>true</trueValue>
<falseValue>false</falseValue>
<nullValue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</root>