[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Validating XML: specifying schema externally

  • To: Xml-Dev <xml-dev@l...>
  • Subject: Validating XML: specifying schema externally
  • From: "Mayne, Peter" <PeterMayne@a...>
  • Date: Thu, 13 Mar 2003 14:01:26 +1100

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.


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.

Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.