XSD验证程序给出org.xml.sax.SAXParseException:schema_reference.4错误

时间:2014-05-15 09:04:12

标签: java xml xsd

当我尝试加载xsd文件时,我收到以下异常。

例外:

    org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document     
   'jar:file:/XXX.jar!/lib/yyyy.jar!/resources/copyprocess.xsd', because 1) could not  
    find the document; 2) the document could not be read; 3) the root element of the 
    document is not <xsd:schema>.
     at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown 
    Source)
    at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.reportSchemaError(Unknown  
    Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.getSchemaDocument(Unknown  
    Source)
    at org.apache.xerces.impl.xs.traversers.XSDHandler.parseSchema(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaLoader.loadSchema(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at org.apache.xerces.impl.xs.XMLSchemaLoader.loadGrammar(Unknown Source)
    at org.apache.xerces.jaxp.validation.XMLSchemaFactory.newSchema(Unknown Source)
    at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:594)
    at javax.xml.validation.SchemaFactory.newSchema(SchemaFactory.java:626)   

Java代码

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema =  
    sf.newSchema(getClass().getResource("/resources/copyprocess.xsd"));

copyprocess.xsd

     <?xml version="1.0" encoding="UTF-8"?>
      <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"  
     elementFormDefault="qualified" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
     jaxb:version="2.1">

我已经检查了yyyy.jar.resources中存在的文件,root元素如上所述。我如何继续这个?

1 个答案:

答案 0 :(得分:0)

问题是在路径中有两个罐子(即jar中的jar),我正在做getResource。在jar的jar场景中,getResourceAsStream会起作用。