Unable to validate XML Schema with SAX Parser:

Discussion in 'Java' started by fhuddles, Feb 13, 2005.

  1. fhuddles

    fhuddles Guest

    I'm trying to validate an XML document with a schema, but the program
    always throws the SAXException whenever it tries to set the validation
    feature on the parser or the underlying reader. So as far as I can
    tell, the parser isn't doing any validation at all - just parsing the
    document. I've seen a few different approaches, and have tried them,
    but none have worked. Here's what I've got, and I've left statements
    from different approaches in there, commented out.

    Can anyone tell me what's wrong and perhaps provide code that actually


    Frank Huddleston

    import java.io.IOException;

    import javax.xml.parsers.FactoryConfigurationError;
    import javax.xml.parsers.ParserConfigurationException;
    import javax.xml.parsers.SAXParser;
    import javax.xml.parsers.SAXParserFactory;

    import org.xml.sax.SAXException;
    import org.xml.sax.SAXNotRecognizedException;
    import org.xml.sax.SAXNotSupportedException;
    import org.xml.sax.XMLReader;
    import org.xml.sax.helpers.DefaultHandler;
    import org.xml.sax.helpers.XMLReaderFactory;

    // import com.sun.org.apache.xerces.internal.parsers.SAXParser;

    public class SAXParserDemo {

    static final String JAXP_SCHEMA_LANGUAGE =

    static final String W3C_XML_SCHEMA =

    public void performDemo( String uri ) {

    System.out.println("Parsing XML file: " + uri + "\n\n");

    //-- Get instances of our handlers.
    DefaultHandler contentHandler = new MyContentHandler();
    // ErrorHandler errorHandler = new MyErrorHandler();
    try {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser parser = factory.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    // DefaultHandler handler = contentHandler;
    try {
    parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);
    // reader.setFeature("http://xml.org/sax/features/validation",

    catch (SAXException x) {
    // Happens if the parser does not support JAXP 1.2
    System.err.println("Note: Schema Validation property
    can't be set on parser.");

    parser.parse(uri, contentHandler);
    fhuddles, Feb 13, 2005
    1. Advertisements

  2. fhuddles

    fhuddles Guest

    I'm going to answer my own post here, because I have had success with
    this - but using another machine with another version of java. The
    version which doesn't work is on a Mac running the following version:
    java version "1.4.2_05"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-141.3)
    Java HotSpot(TM) Client VM (build 1.4.2-38, mixed mode)

    I tried compiling the same code on a linux computer running this
    java version "1.5.0"
    Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-b64)
    Java HotSpot(TM) Client VM (build 1.5.0-b64, mixed mode, sharing)

    and it worked fine. No complaints about setting the features, with
    either method; both the reader.setFeature and the parser.setProperty
    worked and produced a warning message when something which did not
    agree with the schema was encountered.
    The imports were the same: from javax.xml.parsers, so it appears that
    the problem is caused by a lack of support for the schema validation
    features in the older version of java.
    fhuddles, Feb 15, 2005
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page