GeeCON Prague 2014*
The moose likes XML and Related Technologies and the fly likes Problem validating schema Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Problem validating schema" Watch "Problem validating schema" New topic
Author

Problem validating schema

Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
Guys I really need ur help here..
I am trying to validate a schema file using JAXP1.2.. Here is my code .. I have no clue why I get this error
Schema_reference 4: Failed to read schema document 'null'

//constants
static String JAXP_SCHEMA_LANGUAGE =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";
static String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";
static final String JAXP_SCHEMA_SOURCE =
"http://java.sun.com/xml/jaxp/properties/schemaSource";

try
{
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
spf.setValidating(true);
SAXParser saxParser = spf.newSAXParser();
try
{
saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
}
catch (SAXNotRecognizedException x)
{
throw new RuntimeException( "Parser doesn't support XML Schema. "+x.getMessage());
}
saxParser.setProperty(JAXP_SCHEMA_SOURCE, new File("c:\\projects\\xml Mssaging\\bin\\XMLSchema.xsd"));
// Validate
XMLReader xmlReader = saxParser.getXMLReader();
errorChecker = new ErrorChecker( verboseErrors );
xmlReader.setErrorHandler( errorChecker );

// Validate
xmlReader.parse(new InputSource(new FileReader(inputFile)));

// If error happened
if ( errorChecker.errorHappened )
{
isValid = false;
// Suggest "verboseerrors" att if not on
if ( ! verboseErrors )
{
System.out.println( "Error. Set SchemaValidate's 'verbose errors' attribute to true for a more detailed description on warnings.");
}
}
}
catch (IOException e)
{
isValid = false;
System.err.println("Error: IO Exception.");
e.printStackTrace();
}
catch (SAXException e)
{
isValid = false;
System.err.println("Error: SAXException.");
e.printStackTrace();
}
catch (ParserConfigurationException e)
{
isValid = false;
System.err.println("Error: ParserConfigurationException.");
e.printStackTrace();
}

I would really appreciate any help...This thing has been bugging me for a few days now..
Thanks,
Roopa
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi Roopa,
I tried your code wich works perfectly for me. The thing that might not be working is the location of the xsd file (this is actually the error reported by your code: failed to read schema document).
All your have to do is check the location of the file. The path "c:\\projects\\xml Mssaging\\bin\\XMLSchema.xsd" might actually be wrong (don't you miss a e in Mssaging for instance?).
Cheers
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
For reference, I used the following files for testing:
- roopa.xml
- roopa.xsd
- Roopa.java
Contents of roopa.xml:

Contents of roopa.xsd:

Contents of Roopa.java:

Hope this helps
 
GeeCON Prague 2014
 
subject: Problem validating schema