Search code examples
xsdxercesjava-6

XSD SchemaFactory error - Java 6


In the code below, I am trying to validate a XML. Everything works find when i run it from eclipse. When i deploy it on weblogic the code fails with error: "SAX Exception while processingsrc-resolve: Cannot resolve the name XX:sometag to a(n) 'type definition' component."

SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
System.out.println("schemaFactory -> " + schemaFactory.getClass());
Schema schema = schemaFactory.newSchema(resourceHandler.getXSDStreams());
StreamSource xmlContent = new StreamSource(new StringReader(requestXML));
Validator validator = schema.newValidator();
validator.validate(xmlContent);

This is the output generated on eclipse for Schema Factory: schemaFactory -> class com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory

On Weblogic the same code with the same JAR files gives the error mentioned above. The schemaFactory is exactly the same.

Config - 2

I tried setting the System property to load xerces files and added xerces to the classpath

String schemaFactoryProperty = "javax.xml.validation.SchemaFactory:" + XMLConstants.W3C_XML_SCHEMA_NS_URI;
System.setProperty(schemaFactoryProperty,"org.apache.xerces.jaxp.validation.XMLSchemaFactory");  

Still the output for the schema factory remains the same: "schemaFactory -> class com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory" but this time the error changed to:

java.lang.IllegalArgumentException: Source parameter of type 'javax.xml.transform.stream.StreamSource' is not recognized this SchemaFactory.
    at com.sun.org.apache.xerces.internal.jaxp.validation.XMLSchemaFactory.newSchema(XMLSchemaFactory.java:203)
    at com.bt.asi.task.AffectedServiceTask.execute(AffectedServiceTask.java:47)
    at com.bt.asi.service.ASIJMSService.onMessage(ASIJMSService.java:30)
    at... 

I am on Java 6 and WLS 10.3.0 (Java 6) and using Spring framework.


Solution

  • Got it working (after 4 hrs !!!).

    On local why it worked: The Schemas were loaded from a folder so the imports within the XSD like used to work.

    On Server i had put in a jar file so the sourceLocation="SomeFile.xsd" never used to result in a file as the actual path was a URI.

    What fixed it: 1) I changed the sequence in which i load the schemas (from no imports --> all imports) to make sure when the current xsd compiles all dependencies for it are already in the schemaFactory (It was possible as i did not have nexted imports so i had a clear order of what file to load first and so on)

    2) What i could have done as well was - combine all XSDs to one file.

    3) OR I could have kept the XSD in web-inf/ and then looked them up instead of a JAR file. Then the import sourceLocation="" would have found that file.

    Hope this helps someone !!!

    Cheers. Vivek