Ok, having gone away and looked at my problem further, I have now
ammended my original code so I am now using JAXP, but I'm still
struggling, more due to me misunderstanding the documentation I think
as opposed to actual errors in the code. The small section of code
dealing with the parsing is at the bottom of this post.
I am struggling to understand the process of validating an XML document
against a given schema. Currently, I am using SAXParserFactory and
turning validation on by using factory.setValidation(true);
However, from what I can understand from the documentation this just
turns on validation, whether it be against a DTD or any other schema
language. In my application I wish to be able to open an XML file and
it against a W3C Schema as specified by the 'xs:schemaLocation'
attribute within the XML file, which will probably be in a remote
location. From what I have read, it is possible to set the validation
language to W3C Schema but this is only possible by using the
alternative validation method of using properties and features to set
the parser up and not by simply using the setValidation() method. What
is the difference between these methods in terms of validation?
Therefore, my question is what is the best way to make my application
validate the XML document against the W3C schema as given in the XML
file, and how should I go about it? Also, if anyone knows of any
tutorials or similar covering this subject area and could point me in
their direction, I would be most grateful.
Many thanks
Steve
// Create class to parse XML file using JAXP...
class XMLParser
{
// Create control variables for namespace awareness and validation...
public boolean nsAwareness = true;
public boolean validating = true;
public XMLParser()
{
try
{
// Create JAXP SAX parser factory...
SAXParserFactory factory = SAXParserFactory.newInstance();
// Set parsing properties...
factory.setNamespaceAware(nsAwareness);
factory.setValidating(validating);
// Create an instance of the content handler and error handler...
SAXHandler handler = new SAXHandler();
XMLErrorHandler errorHandler = new XMLErrorHandler();
// Create an instance of the SAXParser and set handlers...
XMLReader xmlReader = factory.newSAXParser().getXMLReader();
xmlReader.setContentHandler(handler);
xmlReader.setErrorHandler(errorHandler);
// Initialize InputSource...
InputSource inputSource = new
InputSource(Application.filename.toURI().toString());
inputSource.setSystemId(Application.filename.toURI().toString());
// Parse the file...
xmlReader.parse(inputSource);
}
catch (IOException fileError )
{
fileError.printStackTrace();
}
catch (SAXException SAXError)
{
SAXError.printStackTrace();
}
catch (ParserConfigurationException parserError)
{
parserError.printStackTrace();
}
}
}