SAX parser problem

S

steve_marjoribanks

I am writing an application which uses the following class to parse and
validate an xml file.
The 'filename' in the InputSource line is the file which is selected to
be opened from the JFileChooser in another class. The problem is that
it seems to open the file fine and it seems to find well-formedness
errors but I don't think it is actually validating against a schema at
all. I have tried opening an XML file which I have put validity (as
defined by the accompanying schema) errors into and my application just
parses it and doesn't give any errors or anything. I have set up a
simple Handler so that I know when the parsing starts and finishes so I
know that the file has been parsed but not validated for some reason.

Also, when I open an XML which has an 'import' statement to import a
schema from a remote location I get a load of runtime errors which I
think are to do with not being able to find the file because the first
line says 'Connection timed out' or something similar. What catch
statement do I need to include to ensure that if the file cannot be
found on the remotes server that it does not throw a load of
exceptions? It does seem kinda strange though that the parser should be
trying to find a schema if it doesn't seem to be validating against it
anyway?!

Thanks

Steve


class XMLParser
{
// Create control variables for namesapce awareness and validation...
public boolean awareness = true;
public boolean validating = true;

public XMLParser()
{
try
{
// Create SAX 2 parser factory...
SAXParserFactory factory = SAXParserFactory.newInstance();
// Set parsing properties...
factory.setNamespaceAware(awareness);
factory.setValidating(validating);
// Create an instance of the content handler...
SAXHandler handler = new SAXHandler();
// Create an instance of the SAXParser
SAXParser saxParser = factory.newSAXParser();
// Initialize InputSource and FileReader...
InputSource inputSource = new
InputSource(Application.filename.toURI().toString());
inputSource.setSystemId(Application.filename.toURI().toString());
// Parse the file...
saxParser.parse(inputSource, handler);
}
catch (IOException fileError )
{
fileError.printStackTrace();
}
catch (SAXException SAXError)
{
SAXError.printStackTrace();
}
catch (ParserConfigurationException parserError)
{
parserError.printStackTrace();
}
}
}
 
R

Raymond DeCampo

I am writing an application which uses the following class to parse and
validate an xml file.
The 'filename' in the InputSource line is the file which is selected to
be opened from the JFileChooser in another class. The problem is that
it seems to open the file fine and it seems to find well-formedness
errors but I don't think it is actually validating against a schema at
all.

I am not sure but I think there may be extra properties or features one
must set to convince the parser to use a schema to validate. Consult
the documentation for your parser.

HTH,
Ray
 
S

steve_marjoribanks

Ahh, found the problem now, I was mixing up using SAX as part of JAXP
and SAX by itself. And yes there are other properties I need to set,
and I need to use an ErrorHandler as well.
 
S

steve_marjoribanks

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();
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top