Validate xml against the schema : Schema « XML « Java






Validate xml against the schema

     

import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class SchemaParserXerces implements ErrorHandler {

  public void warning(SAXParseException e) throws SAXException {
    throw e;
  }

  public void error(SAXParseException e) throws SAXException {
    throw e;
  }

  public void fatalError(SAXParseException e) throws SAXException {
    throw e;
  }

  public static void main(String[] args) throws Exception {
    DOMParser d = new DOMParser();
    d.setFeature("http://xml.org/sax/features/validation", true);
    d.setFeature("http://apache.org/xml/features/validation/schema", true);
    d.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
    d.setErrorHandler(new SchemaParserXerces());
    d.parse(args[0]);
    System.out.println("Parsed successfully");
  }

}

   
    
    
    
  








Related examples in the same category

1.Validate xml against XML Schema
2.Create XML validator from XML schema
3.Encoder and Decoder for XML element and attribute names
4.XML constants
5.Resolve relative URIs and SystemID strings into absolute URIs
6.Provides a trace of the schema type information for elements and attributes in an XML document.
7.Use JAXP Validation API to create a validator and validate input from a DOM which contains inline schemas and multiple validation roots.
8.Return the first element child with the specified qualified name.
9.XSDWriter is a helper class to produce xml schemas in Java.