我的 XSD 文件中有以下代码片段。我希望它生成一个包含两个元素 STANDALONE 和 CONNECTED 的 Java 枚举。
<xs:attribute name="Sample">
<xs:annotation>
<xs:documentation>A sample</xs:documentation>
</xs:annotation>
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Standalone"/>
<xs:enumeration value="Connected"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
我正在使用 jaxb2-maven-plugin 版本 3.1.0
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<executions>
<execution>
<id>gen-infrastructure</id>
<goals>
<goal>xjc</goal> <!-- Generates Java sources from XML Schema(s). -->
</goals>
<configuration>
<locale>en,US</locale>
<xjbSources>
<xjbSource>src/main/resources/schema/xsd/sample.xjb</xjbSource>
</xjbSources>
<sources>
<source>src/main/resources/schema/xsd/sample.xsd</source>
</sources>
<!-- The package of your generated sources -->
<packageName>com.company.sample.model.pojos</packageName>
</configuration>
</execution>
</executions>
<configuration>
<noGeneratedHeaderComments>true</noGeneratedHeaderComments>
<clearOutputDir>false</clearOutputDir>
</configuration>
</plugin>
我在 GitHub 上寻找了同样的问题。我问我公司的另一位员工是否有解决方案或看到出了什么问题。
尝试
<xs:attribute name="Sample" type="MyEnum">
<xs:annotation>
<xs:documentation>A sample</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:simpleType name="MyEnum">
<xs:restriction base="xs:string">
<xs:enumeration value="Standalone"/>
<xs:enumeration value="Connected"/>
</xs:restriction>
</xs:simpleType>
我不知道你是否使用命名空间。也许您必须在属性定义中的 type 属性中添加一个。