JAXB XML必需的字段验证

时间:2017-01-05 11:28:16

标签: java xml validation jaxb

我正在尝试验证传入的XML字符串[数据类型,强制性,长度验证]。

首先我通过使用JAXB将传入的xml转换为相应的Java POJO对象,我需要在解组时使用JAXB验证。我传入的xml字符串看起来像。

<Message>
    <Body>
        <Id></Id> // This is Mandatory field but incoming value is empty also it is integer field
        <Name>Siva</Name> // Input is valid
        <Age>ss</Age> // This is integer fields but incoming value is string
        <Mobile>999999999999999</Mobile> // length should be 10.. but is is exceeded
    </Body>
</Message>

我的XSD是:

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

<xs:element name="Mobile">
 <xs:simpleType>
  <xs:restriction base="xs:long">
   <xs:minLength value="10"/>
   <xs:maxLength value="10"/>
  </xs:restriction>
 </xs:simpleType>
</xs:element>

  <xs:element name="Message">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Body">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:int" name="Id" minOccurs="1"/>
              <xs:element type="xs:string" name="Name"/>
              <xs:element type="xs:string" name="Age"/>
              <xs:element type="xs:long" name="Mobile"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

我的JAXB Java代码:

JAXBContext context = JAXBContext.newInstance(Employee.class);

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
        Schema schema = sf.newSchema(new File("resources/Employee.xsd"));

        Unmarshaller unmarshaller = context.createUnmarshaller();

        unmarshaller.setSchema(schema);
        unmarshaller.setEventHandler(new EmployeeValidationEventHandler());

        Employee employee = (Employee) unmarshaller.unmarshal(new File("resources/Employee.xml"));

我尝试了一切,但它不适合我。请帮助我。

1 个答案:

答案 0 :(得分:1)

对于移动设备,您的XSD中没有正确的类型参考。 试试吧。

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Message">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Body">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element type="xs:int" name="Id" minOccurs="1"/>
                            <xs:element type="xs:string" name="Name"/>
                            <xs:element type="xs:string" name="Age"/>
                            <xs:element name="Mobile">
                                <xs:simpleType>
                                    <xs:restriction base="xs:long">
                                        <xs:minLength value="10"/>
                                        <xs:maxLength value="10"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>