0
我正在嘗試驗證傳入的XML字符串[數據類型,強制性,長度驗證]。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"));
我什麼都試過,但它不是我的作品。請幫助我一個人。
注意到&謝謝..但是必填字段驗證還沒有工作。 – DEADEND