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