Hello,

I try to validate an XML document (a DOM document built in memory) using a
Validator, I need the
Xerces Validator for knowing the exact DOM part having error. 

Using Xerces 2.9.1, I can't validate my document, each time it returns me
that It can't find the declaration of the Root element. If I switch to the
default the JDK 5 validator, it works ??

Someone have an idea ? Why Xerces can't find the root declaration ? I'm sure
my XSD document correct and the path is correct too. Tracing It seems Xerces
doesn't load correctly the grammar in the XMLSchema.

Thank you for your help !

import java.io.File;
import java.io.IOException;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.w3c.dom.Document;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class XSDValidator implements ErrorHandler {

        private Validator validator = null;
        
        public XSDValidator( String schemaURI ) throws Exception {
                
                System.setProperty( 
                                "javax.xml.validation.SchemaFactory:" +
XMLConstants.W3C_XML_SCHEMA_NS_URI, 
                                
"org.apache.xerces.jaxp.validation.XMLSchemaFactory" );

                SchemaFactory schemaFactory = SchemaFactory.newInstance(
                                XMLConstants.W3C_XML_SCHEMA_NS_URI );
                Schema schema = null;
                if ( schemaURI.indexOf( "://" ) > -1 )
                        schema =
                                schemaFactory.newSchema( 
                                        new URL( schemaURI ) );
                else
                        schema = 
                                schemaFactory.newSchema(
                                                new File( schemaURI )
                                );

                validator = schema.newValidator();
                validator.setErrorHandler( this );
                
        }

        private boolean error = false;
        private String errorMessage = null;
        
        public boolean validate( Document source ) {
                error = false;
                errorMessage = null;
                try {

                        validator.validate( new DOMSource( source ) );

                } catch (SAXException e) {
                        errorMessage = e.getMessage();
                        error = true;
                } catch (IOException e) {
                        errorMessage = e.getMessage();
                        error = true;
                }
                return !error;
        }

        public String getErrorMessage() { return errorMessage; }
        
        public void error(SAXParseException exception) throws SAXException {
                if ( !error ) {
                        errorMessage = exception.getMessage();
                        // validator.getProperty(
"http://apache.org/xml/properties/dom/current-element-node"; );
                        error = true;
                }
        }

        public void fatalError(SAXParseException exception) throws SAXException 
{
                if ( !error ) {
                        errorMessage = exception.getMessage();
                        error = true;
                }               
        }

        public void warning(SAXParseException exception) throws SAXException {
        }
        
-- 
View this message in context: 
http://www.nabble.com/Can%27t-Validate-with-the-XMLSchemaFactory-tp21623156p21623156.html
Sent from the Xerces - J - Users mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: j-users-unsubscr...@xerces.apache.org
For additional commands, e-mail: j-users-h...@xerces.apache.org

Reply via email to