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);
}
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);
}