Unable to validate XML Schema with SAX Parser:

F

fhuddles

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
works?

thanks,

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 =
"http://java.sun.com/xml/jaxp/properties/schemaLanguage";

static final String W3C_XML_SCHEMA =
"http://www.w3.org/2001/XMLSchema";

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();
factory.setNamespaceAware(true);
factory.setValidating(true);
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",
true);
//
reader.setFeature("http://apache.org/xml/features/validation/schema",
true);

}
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.");
System.err.println(x.getMessage());
System.exit(0);
}

parser.parse(uri, contentHandler);
}
 
F

fhuddles

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
version:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,831
Latest member
HealthSmartketoReviews

Latest Threads

Top