我的项目有一个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 FILE(以上代码试图阅读):
<?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。但是我得到了这个例外。
请帮我解决这个问题
由于 阿伦
答案 0 :(得分:0)
问题已解决,
这是因为,在jboss-5中,使用getResource()无法读取WAR文件中的文件,因此没有正确的文件绑定SchemaFactory
我改变了文件的路径,现在它工作得很酷!!