下面是一个有帮助的例子。
XML 架构 - schema.xsd
JAXBElement<Boolean>
当您有一个既是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
elemnent 生成的类的样子:
@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>