wood burning stoves 2.0*
The moose likes XML and Related Technologies and the fly likes Schema Validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Schema Validation" Watch "Schema Validation" New topic
Author

Schema Validation

Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
I have problems validating schema using JAXP1.2..
I think I am not doing something right..Here is my code..
//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 messaging\\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();
}


DO I have to implement the EntityResolver interface.. If so why?
Thanks,
Roopa
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Schema Validation