You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by alex77 <al...@hotmail.com> on 2009/01/23 12:53:19 UTC

Can't Validate with the XMLSchemaFactory

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-unsubscribe@xerces.apache.org
For additional commands, e-mail: j-users-help@xerces.apache.org


Re: Can't Validate with the XMLSchemaFactory

Posted by alex77 <al...@hotmail.com>.
Thank you Michael, Indeed I had to set the namespace aware even for a
document without namespace.

Best regards,

A.Brillant


Michael Glavassevich-3 wrote:
> 
> XML schema validation requires namespace support. Check that the DOM
> you're
> passing in was created by a namespace-aware parser or created using
> namespace-aware methods (e.g. Document.createElementNS()).
> 
> Michael Glavassevich
> XML Parser Development
> IBM Toronto Lab
> E-mail: mrglavas@ca.ibm.com
> E-mail: mrglavas@apache.org
> 
> alex77 <al...@hotmail.com> wrote on 01/23/2009 06:53:19 AM:
> 
>> 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-unsubscribe@xerces.apache.org
>> For additional commands, e-mail: j-users-help@xerces.apache.org
>>
> 

-- 
View this message in context: http://www.nabble.com/Can%27t-Validate-with-the-XMLSchemaFactory-tp21623156p21624612.html
Sent from the Xerces - J - Users mailing list archive at Nabble.com.


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


Re: Can't Validate with the XMLSchemaFactory

Posted by Michael Glavassevich <mr...@ca.ibm.com>.
XML schema validation requires namespace support. Check that the DOM you're
passing in was created by a namespace-aware parser or created using
namespace-aware methods (e.g. Document.createElementNS()).

Michael Glavassevich
XML Parser Development
IBM Toronto Lab
E-mail: mrglavas@ca.ibm.com
E-mail: mrglavas@apache.org

alex77 <al...@hotmail.com> wrote on 01/23/2009 06:53:19 AM:

> 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-unsubscribe@xerces.apache.org
> For additional commands, e-mail: j-users-help@xerces.apache.org
>