@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