为什么JAXB不想验证

  • 我编写了一些Java类并使用JAXB注释对它们进行了注释。
  • 之后,我使用schemagen生成一个xsd。
  • 然后我构建一个对象图并将其编组为一个xml文件。
  • 我修改了xml文件,使其无效。
  • 我希望使用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);
    

    书面的XML开始是这样的:

     <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
     <ns2:customer xmlns:ns2="http://bla.com/">
    

    即使附加的ValidationEventCollector也没有提供任何信息:

     unmarshaller.setEventHandler(new JAXBEventCollector());
    

    JAXBEventCollector是:

     class JAXBEventCollector extends ValidationEventCollector
     {
       @Override
       public boolean handleEvent(ValidationEvent event)
       {
           System.out.println(event.getLocator());
           return true;
       }
     }
    

    你的代码应该工作。 有几件事需要注意:

  • 您的模式的URL是否回到空?
  • 最后一行是“JAXB.unmarshal(file,Customer.class)”的错字,或者是JAXB另一个没有设置模式的解组器。
  • 下面是一段代码,当无效的XML被解组时,肯定会引发错误。 此代码可与MOXy和Metro(RI)JAXB实现一起正常工作。

    public static void main(String[] args) throws Exception  {
        SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI);
        File xsd = new File("customer.xsd");
        Schema schema = sf.newSchema(xsd); 
        JAXBContext context = JAXBContext.newInstance(Customer.class);
        Unmarshaller unmarshaller = context.createUnmarshaller(); 
        unmarshaller.setSchema( schema ); 
    
        FileInputStream xml = new FileInputStream("invalid.xml");
        unmarshaller.unmarshal(xml);
    }
    

    对于Metro,错误如下所示:

    Exception in thread "main" javax.xml.bind.UnmarshalException
     - with linked exception:
    [org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'phone-numbers'. One of '{phoneNumbers}' is expected.]
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:315)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:514)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:215)
        at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:184)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
        at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:184)
        at example.gettingstarted.Demo2.main(Demo2.java:23)
    

    使用MOXy时,错误如下所示:

    Exception in thread "main" javax.xml.bind.UnmarshalException
     - with linked exception:
    [Exception [EclipseLink-25004] (Eclipse Persistence Services - 2.0.3.qualifier): org.eclipse.persistence.exceptions.XMLMarshalException
    Exception Description: An error occurred unmarshalling the document
    Internal Exception: org.xml.sax.SAXParseException: cvc-complex-type.2.4.a: Invalid content was found starting with element 'phone-numbers'. One of '{phoneNumbers}' is expected.]
        at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:114)
        at example.gettingstarted.Demo2.main(Demo2.java:23)
    
    链接地址: http://www.djcxy.com/p/62963.html

    上一篇: Why doesn't JAXB want to validate

    下一篇: Mapping Java with JAXB annotations to XSD keys/keyrefs