@XmlElement
註釋具有required
屬性(默認爲false),用於指示需要XML元素(minOccurs
> 0)。您可以使用java.lang.reflect
API從該類中獲取此值。
XML模式
在foo
元件下方的XML模式是必需的,bar
元件是沒有的。
<?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">
<complexType name="root">
<sequence>
<element name="foo" type="string"/>
<element name="bar" type="string" minOccurs="0"/>
</sequence>
</complexType>
</schema>
生成類
下面是從XML模式生成的類。我們看到foo
字段用@XmlElement(required=true)
進行了註釋,並且bar
字段沒有。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "root", propOrder = {
"foo",
"bar"
})
public class Root {
@XmlElement(required = true)
protected String foo;
protected String bar;
}
想要測試該類是否包含「@XmlElement(required = true)」,或者您想測試該類的實例是否具有require'name'元素? – 2013-02-26 19:47:17
http://www.mkyong.com/java/jaxb-hello-world-example/# – 2013-02-26 20:00:55
@BlaiseDoughan我需要一種方法來測試該字段是否需要或不在XSD端。在Java生成的類中,沒有任何跡象表明。謝謝! – 2013-02-27 09:04:57