Could you send me a code snippet of all steps needed for schema validation? I am desparate. I am using XmlReader and I am not sure if that makes a difference.
Thanks in advance.
-- DX
Have a look at the following code: note: it is an example, you can modify it to meet your specific needs.
package com.appresso.dataspider.adapter.sap.common;
import org.xml.sax.ErrorHandler; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xml.sax.InputSource;
import org.apache.xerces.parsers.SAXParser;
import java.io.IOException;
public class XMLValidator {
private static final XMLValidator xmlValidator = new XMLValidator();
public static XMLValidator getInstance(){
return xmlValidator;
} private XMLValidator(){}
public void validate(InputSource in, String schemaLocation, String
strNamespace)
throws SAXException, IOException
{
SAXParser reader = new SAXParser(); // Request validation
reader.setFeature("http://xml.org/sax/features/validation", true); //set the parser's property: the target namespace, and schema location
if(!strNamespace.equals(""))
reader.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation",
strNamespace+" "+schemaLocation);
else
reader.setProperty("http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation",
schemaLocation);
// Register the error handler
reader.setErrorHandler(new MyErrorHandler());
reader.parse(in);
}
}class MyErrorHandler implements ErrorHandler {
public void warning(SAXParseException exception) {
String errorReport = "**Parsing Warning**\n" +
" Line: " +
exception.getLineNumber() + "\n" +
" URI: " +
exception.getSystemId() + "\n" +
" Message: " +
exception.getMessage();
// We only give information when a warning arises.
System.out.println(errorReport);
} public void error(SAXParseException exception) throws SAXException {
String errorReport = "**Parsing Error**\n" +
" Line: " +
exception.getLineNumber() + "\n" +
" URI: " +
exception.getSystemId() + "\n" +
" Message: " +
exception.getMessage();
System.out.println(errorReport);
throw new SAXException(errorReport);
} public void fatalError(SAXParseException exception) throws SAXException {
String errorReport = "**Parsing Fatal Error**\n" +
" Line: " +
exception.getLineNumber() + "\n" +
" URI: " +
exception.getSystemId() + "\n" +
" Message: " +
exception.getMessage(); System.out.println(errorReport);
throw new SAXException(errorReport);
}
}-------
Xuemin Guan
Software Engineer
Appresso SpA (www.appresso.com)
Tel: +81-3-4412-7790(Direct)
+81-3-4412-7700(Represent)
Email: [EMAIL PROTECTED]
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
