Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

SAX parser problem

Thread view: 
steve_marjoribanks@hotmail.com - 18 Feb 2006 16:32 GMT
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();
        }
    }
}
Raymond DeCampo - 18 Feb 2006 18:59 GMT
> I am writing an application which uses the following class to parse and
> validate an xml file.
[quoted text clipped - 3 lines]
> 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
Signature

This signature intentionally left blank.

steve_marjoribanks@hotmail.com - 19 Feb 2006 19:11 GMT
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.
steve_marjoribanks@hotmail.com - 19 Feb 2006 21:08 GMT
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();
        }
    }
}


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.