OASIS Mailing List ArchivesView the OASIS mailing list archive below
or browse/search using MarkMail.


Help: OASIS Mailing Lists Help | MarkMail Help



   Validating XML: specifying schema externally

[ Lists Home | Date Index | Thread Index ]

I'm trying to use org.apache.xerces.parsers.SAXParser from Java to validate
an XML document.

If the document starts:

<?xml version="1.0"?>

then the code below does the right thing (assuming that PO.xsd is present),
such that if I change <Header></Header> to <xHeader></xHeader>, the document
won't validate.

However, the incoming documents do not have the namespace declarations in
them, so I'm attempting to validate them by specifying the schema from Java.
However, this isn't working properly: the document will be parsed without
errors, even if it contains <xHeader></xHeader>.

What do I have to do to force the document to be validated against a schema
that I specify?


Partial code:

    static final String DEFAULT_PARSER_NAME =
    static final String VALIDATION_FEATURE =
    static final String SCHEMA_VALIDATION_FEATURE =
    static final String DYNAMIC_VALIDATION_FEATURE =
    static final String SCHEMA_LOCATION_PROPERTY =
    static final String SCHEMA_FULL_CHECKING_FEATURE =

    public static void main(String[] args)
       if(args.length != 1)
            System.err.println("Usage: java ParseXML <filename>");
        XMLReader parser = null;
            parser = XMLReaderFactory.createXMLReader(DEFAULT_PARSER_NAME);
        catch (Exception e)
            System.err.println("error: Unable to instantiate parser (" +

            // try various features/properties
            parser.setFeature(VALIDATION_FEATURE, true);
            parser.setFeature(SCHEMA_VALIDATION_FEATURE, true);
            //parser.setFeature(SCHEMA_FULL_CHECKING_FEATURE, true);
            parser.setFeature(DYNAMIC_VALIDATION_FEATURE, true);
            parser.setErrorHandler(new ErrorHandler()
                public void fatalError(SAXParseException spe)
                    throws SAXException
                    printError("Fatal Error", spe);
                public void error(SAXParseException spe)
                    throws SAXParseException
                    printError("Error", spe);
                public void warning(SAXParseException spe)
                    throws SAXParseException
                    printError("Warning", spe);
            if(false) // not using this at the moment
                parser.setEntityResolver(new EntityResolver()
                    public InputSource resolveEntity (String publicId,
String systemId)
                        System.out.println("publicId[" + publicId + "]");
                        System.out.println("systemId[" + systemId + "]");
                        return new InputSource();
        catch(SAXNotRecognizedException e)
            System.err.println("not recognised: " + e);
        catch(SAXNotSupportedException snse)
            System.err.println("not supported: " + snse);
            System.out.println("begin parsing");
            System.out.println("end parsing");
        catch(Exception e)
            System.err.println("Parsing error: " + e);

Peter Mayne
Technology Consultant
Spherion Technology Solutions
Level 1, 243 Northbourne Avenue, Lyneham, ACT, 2602
T: 61 2 62689727  F: 61 2 62689777
The information contained in this email and any attachments to it:

(a) may be confidential and if you are not the intended recipient, any interference with, 
use, disclosure or copying of this material is unauthorised and prohibited; and

(b) may contain personal information of the recipient and/or the sender as defined 
under the Privacy Act 1988 (Cth). Consent is hereby given by the recipient(s) to 
collect, hold and use such information and any personal information contained in a 
response to this email, for any reasonable purpose in the ordinary course of 
business, including forwarding this email internally or disclosing it to a third party. All 
personal information collected by Spherion will be handled in accordance with 
Spherion's Privacy Policy. If you have received this email in error, please notify the 
sender and delete it.

(c) you agree not to employ or arrange employment for any candidate(s) supplied in 
this email and any attachments without first entering into a contractual agreement with 
Spherion. You further agree not to divulge any information contained in this document 
to any person(s) or entities without the express permission of Spherion.


News | XML in Industry | Calendar | XML Registry
Marketplace | Resources | MyXML.org | Sponsors | Privacy Statement

Copyright 2001 XML.org. This site is hosted by OASIS