我希望使用xsd,希望JAXB解组失败.但事实并非如此.为什么?
JAXB正在读取一个模式(如果模式XML是错误的,JAXB给出了一个异常),但它接触到JAXB在读取时忽略了模式.
SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(getClass().getResource( "/schema1.xsd"));
JAXBContext context = JAXBContext.newInstance(Customer.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
unmarshaller.setSchema( schema );
Customer c = JAXB.unmarshal(file, Customer.class);
Run Code Online (Sandbox Code Playgroud)
编写的XML就是这样开始的:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:customer xmlns:ns2="http://bla.com/">
Run Code Online (Sandbox Code Playgroud)
即使附加的ValidationEventCollector也没有提供任何信息:
unmarshaller.setEventHandler(new JAXBEventCollector());
Run Code Online (Sandbox Code Playgroud)
JAXBEventCollector是:
class JAXBEventCollector extends ValidationEventCollector
{
@Override
public boolean handleEvent(ValidationEvent event)
{
System.out.println(event.getLocator());
return true;
}
}
Run Code Online (Sandbox Code Playgroud)