2012-03-16 78 views
0

我的項目有一個JAXWS類,其中讀取了XSD並最終通過另一種方法進行了驗證。嘗試讀取xsd時出現SAXParserException

這裏是Java代碼在那裏讀出的XSD

private static final ThreadLocal <DocumentBuilder> parser = 
      new ThreadLocal <DocumentBuilder>() { 
        @Override protected DocumentBuilder initialValue() { 
          DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
          SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 
          // loading Schema file 
          //System.out.println("Hi .. am in parser"); 
           DocumentBuilder parserBuilder = null; 
           if(finalXsd !=null){ 
           logger.info("finalXsd FILE"+finalXsd);       
           Source schemaFile = new StreamSource(new File(finalXsd)); 

           try{ 
            logger.info ("place-0") ; 
            Schema schema = factory.newSchema(schemaFile); 
            logger.info ("place-1") ; 
            docBuilderFactory.setValidating(false); // true is used for DTD based validation, for schema validation set to false 
            logger.info ("place-2") ; 
            docBuilderFactory.setSchema(schema); 
            logger.info ("place-3") ; 
            parserBuilder = docBuilderFactory.newDocumentBuilder(); 
            logger.info ("place-4") ; 
            parserBuilder.setErrorHandler(new JythonResponseErrorHandler()); 
            logger.info ("place-5") ; 
           }catch(SAXException s)       
           { 
            logger.error("SAXException in ThreadLocal."); 
            s.printStackTrace() ; 
            return null; 
           } catch (ParserConfigurationException e) {        
            e.printStackTrace(); 
            logger.error("ParserConfigurationException in ThreadLocal."); 
            return null; 
           } 


          } 
          return parserBuilder; 

        } 
      }; 

XSD文件(它上面的代碼試圖讀取):

<?xml version="1.0" encoding="UTF-8"?> 

<!--W3C Schema generated by XMLSpy v2010 rel. 2 (http://www.altova.com) --> 

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="TAG"> 
     <xs:complexType> 
      <xs:attribute name="name" use="required" type="xs:string" /> 
      <xs:attribute name="value" use="optional" type="xs:string" /> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="STRING"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="EN" /> 
       <xs:element ref="ES" /> 
      </xs:sequence> 
      <xs:attribute name="name" use="required" type="xs:string"> 
      </xs:attribute> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="FRACTION"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="N" /> 
       <xs:element ref="D" /> 
      </xs:sequence> 
      <xs:attribute name="name" use="required" > 
      </xs:attribute> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="RESULTLIST"> 
     <xs:complexType> 
     <xs:sequence> 
       <xs:element ref="RESPONSE" maxOccurs="unbounded" /> 
     </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="RESPONSE"> 
     <xs:complexType mixed="true" > 
       <xs:sequence> 
        <xs:any minOccurs="0" maxOccurs="unbounded"/>    
       </xs:sequence>  
     </xs:complexType> 
    </xs:element> 

    <xs:element name="PLURAL"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="STRING" /> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="NAME"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="STRING" /> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="INTEGER"> 
    </xs:element> 

    <xs:element name="ID" type="xs:int"> 
    </xs:element> 

    <xs:element name="GENDER"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="STRING" /> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="BOOL"> 
     <xs:complexType> 
      <xs:simpleContent> 
       <xs:extension base="xs:boolean"> 
        <xs:attribute name="name" use="required" type="xs:string"> 
        </xs:attribute> 
       </xs:extension> 
      </xs:simpleContent> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="FILE_ID" type="xs:int"> 
    </xs:element> 

    <xs:element name="ES" type="xs:string" /> 

    <xs:element name="EN" type="xs:string" /> 
    <xs:element name="N" type="xs:int" /> 
    <xs:element name="D" type="xs:int" /> 
    <xs:element name="DICTIONARY"> 
     <xs:complexType mixed="true"> 
     <xs:choice>   
      <xs:any minOccurs="0" maxOccurs="unbounded"/> 
     </xs:choice>  
      <xs:attribute name="name" use="required" type="xs:string"> 
      </xs:attribute> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="ASSET"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="NAME" /> 
       <xs:element ref="PLURAL" /> 
       <xs:element ref="ALTTEXT" /> 
       <xs:element ref="GENDER" /> 
       <xs:element ref="ID" /> 
       <xs:element ref="FILE_ID" /> 
       <xs:element ref="DICTIONARY" maxOccurs="unbounded" /> 
      </xs:sequence> 
      <xs:attribute name="name" use="required" > 
      </xs:attribute> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="ARRAY"> 
     <xs:complexType> 
      <xs:choice> 
       <xs:element ref="ARRAY" minOccurs="0" maxOccurs="unbounded" /> 
       <xs:element ref="STRING" minOccurs="0" maxOccurs="unbounded"/> 
       <xs:element ref="ASSET" maxOccurs="unbounded"/> 
       <xs:element ref="INTEGER" minOccurs="0" maxOccurs="unbounded"/> 
       <xs:element ref="FRACTION" minOccurs="0" maxOccurs="unbounded"/> 
       <xs:element ref="DICTIONARY" minOccurs="0" maxOccurs="unbounded"/> 
      </xs:choice> 
      <xs:attribute name="name" use="required" type="xs:string"> 
      </xs:attribute> 
     </xs:complexType> 
    </xs:element> 

    <xs:element name="ALTTEXT"> 
     <xs:complexType> 
      <xs:sequence> 
       <xs:element ref="STRING" /> 
      </xs:sequence> 
     </xs:complexType> 
    </xs:element> 

</xs:schema> 

在發送從SOAP-UI的響應,則上面的代碼得到執行並拋出異常

org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document 'file:/D:/softwares/jboss-5.1.0.GA/server/default/deploy/poctest.war/WEB-INF/classes/final.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.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 com.hmco.ssms.services.poc.SoarPocJaxWS$1.initialValue(SoarPocJaxWS.java:59) 
    at com.hmco.ssms.services.poc.SoarPocJaxWS$1.initialValue(SoarPocJaxWS.java:46) 
    at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:141) 
    at java.lang.ThreadLocal.get(ThreadLocal.java:131) 
    at com.hmco.ssms.services.poc.SoarPocJaxWS.getParametersMultiple(SoarPocJaxWS.java:144) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.jboss.wsf.container.jboss50.invocation.InvocationHandlerJSE.invoke(InvocationHandlerJSE.java:108) 
    at org.jboss.ws.core.server.ServiceEndpointInvoker.invoke(ServiceEndpointInvoker.java:222) 
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.processRequest(RequestHandlerImpl.java:474) 
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleRequest(RequestHandlerImpl.java:295) 
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.doPost(RequestHandlerImpl.java:205) 
    at org.jboss.wsf.stack.jbws.RequestHandlerImpl.handleHttpRequest(RequestHandlerImpl.java:131) 
    at org.jboss.wsf.common.servlet.AbstractEndpointServlet.service(AbstractEndpointServlet.java:85) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190) 
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92) 
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126) 
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) 
    at java.lang.Thread.run(Thread.java:662) 

我甚至試圖將XS更改爲XSD在xs d文件。但我仍然得到這個例外。

請幫我在這

感謝 阿倫

+1

拋出異常究竟在哪裏? 您是否驗證過(在代碼中)finalXsd準確表示正確的路徑? 您覈實了XMLConstants.W3C_XML_SCHEMA_NS_URI確實與「http://www.w3.org/2001/XMLSchema」匹配嗎? (好吧,最後一個是有點延伸 - 它應該這樣做)。 – GregHNZ 2012-03-16 08:23:55

+0

在Schema schema = factory.newSchema(schemaFile)中引發異常。 正好在logger.info(「place-0」)之後; 。路徑是正確的格雷格 – Arun 2012-03-16 08:28:44

回答

0

問題是固定的,

這是因爲,在JBoss中-5 WAR文件裏的文件無法使用的getResource read()和因此沒有正確的文件綁定SchemaFactory

我改變了文件的路徑,現在它工作很酷!