JAXB从xsds生成的类总是有@XmlElement(required = true)

时间:2013-03-06 21:35:43

标签: java xml xsd jaxb maven-jaxb2-plugin

我的xml架构定义如下

<?xml version="1.0" encoding="UTF-8"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://poc/"
elementFormDefault="qualified"
targetNamespace="http://poc/"
attributeFormDefault="unqualified"
xmlns:tns="http://poc/">

<xs:simpleType name="custType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Primary"/>
        <xs:enumeration value="Coapplicant"/>
    </xs:restriction>
</xs:simpleType>
<xs:complexType name="addressType">
    <xs:sequence>
        <xs:element name="address" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="state" type="xs:string" minOccurs="0"/>
        <xs:element name="zip" type="xs:string" minOccurs="0"/>
    </xs:sequence>
</xs:complexType>

<xs:element name="request1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="fname" type="xs:string" minOccurs="0"/>
            <xs:element name="lname" type="xs:string" minOccurs="0"/>
            <xs:element maxOccurs="1" name="categoryCode" minOccurs="0">
                <xs:simpleType>
                   <xs:restriction base="xs:integer">
                    <xs:maxLength value="4"/>
                   </xs:restriction>
               </xs:simpleType>
            </xs:element>
            <xs:element name="retiredInd" type="xs:boolean" minOccurs="0"/>
            <xs:element name="custType" type="tns:custType" minOccurs="0"/> 
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:element name="response1">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="addr" type="tns:addressType" minOccurs="0"/>
            <xs:element name="nation" type="xs:token" minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>
</xs:schema>

我使用maven-jaxb2-plugin为这个xsd生成Java类。

<plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <version>0.8.3</version>
    <configuration>
        <schemaDirectory>${basedir}/cfg</schemaDirectory>
        <schemaIncludes>
            <include>xsd/*.xsd</include>
        </schemaIncludes>
        <generateDirectory>${basedir}/src/main/java</generateDirectory>
        <strict>false</strict>
        <extension>true</extension>
        <removeOldOutput>false</removeOldOutput>
        <clearOutputDir>false</clearOutputDir>
        <forceRegenerate>true</forceRegenerate><plugins>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics</artifactId>
                <version>0.6.4</version>
            </plugin>
            <plugin>
                <groupId>org.jvnet.jaxb2_commons</groupId>
                <artifactId>jaxb2-basics-annotate</artifactId>
                <version>0.6.4</version>
            </plugin>
        </plugins>
    </configuration>
    <executions>
        <execution>
        <id>tsys-sources</id>
        <goals>
            <goal>generate</goal>
        </goals>
        </execution>
    </executions>
</plugin>

生成代码后,我发现所有元素都有@XmlElement的注释(required = true)。为什么?我有很多minOccurs =“0”元素。为什么始终在元素上添加required = true。

2 个答案:

答案 0 :(得分:2)

我使用示例中的插件生成了您的示例源代码 我只在

@XmlElement(required = true)
AddressType#address  
AddressType#city

字段。

public class AddressType {

    @XmlElement(required = true)
    protected String address;
    @XmlElement(required = true)
    protected String city;
    protected String state;
    protected String zip;   
    // ...  
}

因此,也请在此字段中添加minOcurrs

答案 1 :(得分:0)

我的binding.xml中的本地配置问题。在清除了一些无用的配置后我修复了它。