我正在使用 xsd,其中我是验证电子邮件
<xsd:element name="Company" type="Company"/>
<xsd:complexType name="Company">
<xsd:sequence>
<xsd:element name="Name" type="xsd:string" minOccurs="1" maxOccurs="1" />
<xsd:element name="Website" type="xsd:string" minOccurs="0" maxOccurs="1" />
<xsd:element name="Email" type="EmailAddress" minOccurs="0" maxOccurs="1" />
</xsd:sequence>
<xsd:attribute name="AccountStatus" type="AccountStatus" use="optional" default="Active" />
</xsd:complexType>
<xsd:simpleType name="EmailAddress">
<xsd:restriction base="xsd:string">
<xsd:pattern value="[A-Za-z0-9_]+([-+.'][A-Za-z0-9_]+)*@[A-Za-z0-9_]+([-.][A-Za-z0-9_]+)*\.[A-Za-z0-9_]+([-.][A-Za-z0-9_]+)*"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="AccountStatus">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="Active"/>
<xsd:enumeration value="Inactive"/>
</xsd:restriction>
</xsd:simpleType>
这是我使用 xjc 代码片段的 pom.xml 文件
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>jaxb2-maven-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<goals>
<goal>xjc</goal>
</goals>
<phase>generate-sources</phase>
</execution>
</executions>
<configuration>
<clearOutputDir>false</clearOutputDir>
<outputDirectory>src/main/java</outputDirectory>
<schemaDirectory>src/main/webapp/schemas</schemaDirectory>
<includeSchema>**/*.xsd</includeSchema>
<bindingDirectory>src/main/resources/bindings</bindingDirectory>
<bindingFiles>binding.xml</bindingFiles>
<enableIntrospection>false</enableIntrospection>
</configuration>
</plugin>
这是我的 binding.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="2.0"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxb:bindings>
<jaxb:globalBindings generateElementProperty="false" />
</jaxb:bindings>
这是生成的 Company.java 类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Company", propOrder = {
"name",
"website",
"email"
})
public class Company {
@XmlElement(name = "Name", required = true)
protected String name;
@XmlElement(name = "Website")
protected String website;
@XmlElement(name = "Email")
protected String email;
@XmlAttribute(name = "AccountStatus")
protected AccountStatus accountStatus;
//setter and getters
}
查看没有适用于我的电子邮件属性的电子邮件模式。为什么?
当我使用 mavencompile 编译代码时,没有 EmailAddress.java 类将模式应用于 Company.java 中的电子邮件属性。 AccountStatus 也是一个简单类型,但 maven 创建了 AccountStatus.java 类。为什么当我使用 maven 编译时,maven 不创建 EmailAddress.java 类?我的模式有问题吗?
谢谢
最佳答案
生成
AccountStatus
是因为相应的架构类型是枚举。对于其他简单类型(即具有模式的类型),将不会生成类。不生成 EmailAddress
类型的类有几个原因:
- 它使类模型变得不必要的大。
- JAXB 不会验证该级别的内容,因此额外生成的类没有任何值(value)。
- 可以通过在
Marshaller
/Unmarshaller
上设置Schema
实例来验证该值(请参阅:http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html)。<
相反,该属性将基于基本架构类型。
关于java - 使用 maven xjc 编译时,JAXB 不会为 xsd 电子邮件模式元素创建类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20883329/