Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > SAX parser problem

Reply
Thread Tools

SAX parser problem

 
 
steve_marjoribanks@hotmail.com
Guest
Posts: n/a
 
      02-18-2006
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();
}
}
}

 
Reply With Quote
 
 
 
 
Raymond DeCampo
Guest
Posts: n/a
 
      02-18-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
--
This signature intentionally left blank.
 
Reply With Quote
 
 
 
 
steve_marjoribanks@hotmail.com
Guest
Posts: n/a
 
      02-19-2006
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.

 
Reply With Quote
 
steve_marjoribanks@hotmail.com
Guest
Posts: n/a
 
      02-19-2006
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();
}
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Xerces-C SAX parser ? christophe_usenet@yahoo.fr XML 0 07-25-2006 09:58 AM
Sax Parser problem : xml encoding of string?? brightoceanlight@hotmail.com Java 5 09-15-2005 03:58 PM
Compile problem using SAX Parser and ant tool Mladen Adamovic Java 0 01-14-2005 06:19 PM
Encoding problem with SAX parser Martin Schlatter Java 2 12-14-2003 10:33 AM



Advertisments