JAXB没有为第二个缺少必需元素
引发ValidationEvent我有像
这样的XSD<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/Employee" xmlns:tns="http://www.example.org/Employee"
elementFormDefault="qualified">
<element name="Employee">
<complexType>
<sequence>
<element name="EmployeeId" type="string"></element>
<element name="Name" type="string"></element>
<element name="SecondName" type="string" minOccurs="0"></element>
<element name="age">
<simpleType>
<restriction base="int">
<minInclusive value="18" />
</restriction>
</simpleType>
</element>
</sequence>
</complexType>
</element>
</schema>
我正在尝试验证和解组的xml
<?xml version="1.0" encoding="UTF-8"?>
<tns:Employee xmlns:tns="http://www.example.org/Employee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/Employee SimpleEmployee.xsd ">
<tns:Junk>Junk</tns:Junk>
<tns:EmployeeId>Emp01</tns:EmployeeId>
<!-- <tns:Name>Hitman</tns:Name> -->
<tns:SecondName>Hardman</tns:SecondName>
<tns:age>13</tns:age>
</tns:Employee>
我有以下代码收集错误消息并打印。
static class ValidationErrorStoringHandler implements ValidationEventHandler
{
List<String> errors = new ArrayList<String>();
public boolean handleEvent( ValidationEvent arg )
{
errors.add( arg.getMessage() );
return true;
}
public void printAllErrors()
{
for( String error : errors )
{
System.out.println( error );
}
}
}
public static void main( String[] args ) throws SAXException, JAXBException, ParserConfigurationException,
IOException
{
JAXBContext context = JAXBContext.newInstance( Employee.class );
Unmarshaller unMarshaller = context.createUnmarshaller();
FileInputStream fs = new FileInputStream(
"C:/ncrdev/workspace/sonartestproject/src/main/resources/SimpleEmployee.xml" );
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
dbFactory.setNamespaceAware( true );
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse( fs );
SchemaFactory sf = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI );
Schema schema = sf.newSchema( new File(
"C:/ncrdev/workspace/sonartestproject/src/main/resources/SimpleEmployee.xsd" ) );
unMarshaller.setSchema( schema );
ValidationErrorStoringHandler handler = new ValidationErrorStoringHandler();
unMarshaller.setEventHandler( handler );
Employee emp = ( Employee ) unMarshaller.unmarshal( doc.getFirstChild() );
handler.printAllErrors();
}
我看到以下输出
cvc-complex-type.2.4.a: Invalid content was found starting with element 'tns:Junk'. One of '{"http://www.example.org/Employee":EmployeeId}' is expected.
unexpected element (uri:"http://www.example.org/Employee", local:"Junk"). Expected elements are <{http://www.example.org/Employee}EmployeeId>,<{http://www.example.org/Employee}SecondName>,<{http://www.example.org/Employee}Name>,<{http://www.example.org/Employee}age>
cvc-minInclusive-valid: Value '13' is not facet-valid with respect to minInclusive '18' for type '#AnonType_ageEmployee'. cvc-type.3.1.3: The value '13' of element 'tns:age' is not valid.
为什么不为缺少的必需元素“Name”引发ValidationEvent?