Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Unable to validate XML Schema with SAX Parser:

Reply
Thread Tools

Unable to validate XML Schema with SAX Parser:

 
 
fhuddles
Guest
Posts: n/a
 
      02-13-2005
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.SAXPars er;

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

 
Reply With Quote
 
 
 
 
fhuddles
Guest
Posts: n/a
 
      02-15-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
web.xml / XML schema issue, why do some XML schema attributes disappear asciz@starmail.com Java 3 02-20-2007 09:56 AM
Validate XML against DTD and/or XML Schema? Reid Priedhorsky Python 2 04-17-2006 08:46 AM
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
[XML Schema] Including a schema document with absent target namespace to a schema with specified target namespace Stanimir Stamenkov XML 3 04-25-2005 09:59 AM
tool to validate xml file against custom XML Schema file Leona XML 9 11-01-2004 09:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57