Question

I was just wondering if someone could give my XML validation code a once over to see if I'm doing it right. Here's the portion of code that is giving me the trouble...

SAXParserFactory factory = SAXParserFactory.newInstance();

SchemaFactory schemaFactory = SchemaFactory
    .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

// *** CODE FAILS ON THE BELOW LINE **/
factory.setSchema(schemaFactory
    .newSchema(new Source[] { new StreamSource(schemaStream) }));

SAXParser parser = factory.newSAXParser();

SAXReader reader = new SAXReader(parser.getXMLReader());
reader.setValidation(false);
reader.setErrorHandler(new ResultProducingErrorHandler());
reader.read(content);

Whenever I run the above code, I get an error along the lines of:

src-resolve: Cannot resolve the name 'ns:myStructure' to a(n) 'type definition' component.

The elements mentioned in the error messages are all ones that are imported into the schema via calls to <xs:import />. The schema seems to validate OK via the W3C XML Schema Validator.

Do I have to include each of these schema's individually or is Java smart enough to go off and fetch these extra schema's too? I tried adding them in the array passed to the newSchema call but that didn't make any difference.

I don't think I can give out the link to the schema, so I'm really just looking for a yes or no regarding if my code looks at least acceptable.

Was it helpful?

Solution

Ensure that the xs:import statements point to paths that are reachable from the current directory of your application. The current directory may not be what you think it is.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top