我正在使用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
}
请参阅我的电子邮件attrubute没有适用的电子邮件模式。为什么?
当我使用maven编译编译我的代码时,没有EmailAddress.java类将模式应用于Company.java中的我的电子邮件属性。 AccountStatus也是一个简单类型,但maven创建AccountStatus.java类。当我使用maven编译时,为什么maven没有创建EmailAddress.java类?我的模式有问题吗?
由于
答案 0 :(得分:0)
AccountStatus
,因为相应的模式类型是枚举。对于其他简单类型(即具有模式的类型),将不会生成类。没有为EmailAddress
类型生成类的原因有几个:
Schema
/ Marshaller
上设置Unmarshaller
的实例来验证该值(请参阅:http://blog.bdoughan.com/2010/12/jaxb-and-marshalunmarshal-schema.html)。相反,该属性将基于基础架构类型。