【问题标题】:SAXParserException while trying to read the xsd尝试读取 xsd 时出现 SAXParserException
【发布时间】:2012-04-01 18:39:54
【问题描述】:

我的项目有一个 JAXWS 类,其中读取 XSD 并最终在另一种方法中验证。

这是他们读取 XSD 的 java 代码

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)

我什至尝试在 xsd 文件中将 XS 更改为 XSD。但我仍然得到这个例外。

请帮帮我

谢谢 阿伦

【问题讨论】:

  • 异常到底是在哪里抛出的?您是否(在代码中)验证了 finalXsd 准确地表示了正确的路径?您是否验证过 XMLConstants.W3C_XML_SCHEMA_NS_URI 确实匹配“w3.org/2001/XMLSchema”? (好吧,最后一个有点牵强 - 它应该这样做)。
  • Schema 抛出异常 schema = factory.newSchema(schemaFile);恰好在 logger.info ("place-0") 之后; .路径是正确的格雷格

标签: xsd xml-parsing saxparser


【解决方案1】:

问题已解决,

这是因为,在 jboss-5 中,无法使用 getResource() 读取 WAR 文件中的文件,因此没有正确的文件绑定 SchemaFactory

我更改了文件的路径,现在它运行良好!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 2019-08-08
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多