Code example for SAXParser: Parsing XML documents

Methods: parse, setProperty

0
    factory.setFeature("http://apache.org/xml/features/validation/schema", true);
    factory.setFeature(
        "http://apache.org/xml/features/validation/schema-full-checking", true); 
 
    SAXParser parser = factory.newSAXParser();
    parser.setProperty(
        "http://java.sun.com/xml/jaxp/properties/schemaLanguage", 
        "http://www.w3.org/2001/XMLSchema"); 
    if(!schema.startsWith("file:")){
      schema = "file://" + schema;
    } 
    parser.setProperty(
        "http://java.sun.com/xml/jaxp/properties/schemaSource", schema);
    parser.setProperty(
        "http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation", 
        schema.replace('\\', '/'));
 
    filename = ProjectUtils.getFilePath(project, filename);
    filename = filename.replace('\\', '/');
 
    ErrorAggregator errorHandler = new ErrorAggregator(filename);
    EntityResolver entityResolver = new EntityResolver(
        FileUtils.getFullPath(filename));
    try{ 
      parser.parse(new File(filename),
          getHandler(null, errorHandler, entityResolver));
    }catch(SAXParseException spe){
      ArrayList<Error> errors = new ArrayList<Error>();
      errors.add(
        new Error(