All,

I have a question about schema validation. I have a feeling that this is something that gets asked a lot but, I haven't been able to find the exact use case that I need help with.

Basically, the project I am working on uses xml to store project data that get generated by our tool. I have created a schema to validate this project xml file. The schema is going to be shipped with the product and will be in the CLASSPATH (possibly in a jar file). I am wondering how I can tell the parser to use the schema file to validate the project file. I would rather not have to specify the schema in the xml of the project file, because then some one could remove the schema declaration. So basically, I want to configure the parser to use a schema that is in the CLASSPATH.

From the java code, the only way I can reference the schema file is something like "/org/foo/schema/project.xsd". This is because I don't know the absolute path of the file I just know that it is in the classpath. I have tried to use the ClassLoader's getResource() method to get the URL of the xsd file, but it still doesn't seem top be working. Can some one point me to an example of how to do this.

Here is what I have that isn't working.

try
{
ClassLoader cl = ConnectionFileParser.class.getClassLoader();
URL url = cl.getResource( CONN_SCHEMA_FILE );
DOMParser parser = new DOMParser();
parser.setFeature("http://xml.org/sax/features/validation";, true);
parser.setFeature("http://xml.org/sax/features/namespaces";, true);
parser.setFeature("http://apache.org/xml/features/validation/schema";, true);
parser.setProperty(
"http://apache.org/xml/properties/schema/external-schemaLocation";,
url.getFile() );
parser.setErrorHandler( eh );
parser.parse( source );
}


if( !eh.isValid() )
{
   throw new IOException( "There were errors when parsing the XML file: \n"
                       + eh.getErrorList() );
}


After parsing, the error handler object contains an error of "Line 3: cvc-elt.1: Cannot find the declaration of element 'Connection'." Which is the root element of the project file. I could be doing this completely wrong. Any help would be great. Thanks


~Mike


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to