Example usage for javax.xml.parsers SAXParserFactory setValidating

List of usage examples for javax.xml.parsers SAXParserFactory setValidating

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParserFactory setValidating.

Prototype


public void setValidating(boolean validating) 

Source Link

Document

Specifies that the parser produced by this code will validate documents as they are parsed.

Usage

From source file:MyErrorHandler.java

static public void main(String[] arg) throws Exception {
    boolean validate = false;
    validate = true;/*ww w . j a v a2 s .  co m*/

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(validate);

    XMLReader reader = null;
    SAXParser parser = spf.newSAXParser();
    reader = parser.getXMLReader();

    reader.setErrorHandler(new MyErrorHandler());
    InputSource is = new InputSource(new StringReader(getXMLData()));
    reader.parse(is);
}

From source file:MainClass.java

public static void main(String args[]) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);/*from www  . j  a  v  a2 s.c  om*/
    spf.setValidating(true);
    System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
    System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");

    SAXParser parser = null;
    try {
        parser = spf.newSAXParser();
    } catch (ParserConfigurationException e) {
        e.printStackTrace(System.err);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
    }
    System.out.println("Parser object is: " + parser);
}

From source file:SAXCheck.java

static public void main(String[] arg) {
    String filename = null;//from   w  w w. jav  a2s. co  m
    boolean validate = false;

    if (arg.length == 1) {
        filename = arg[0];
    } else if (arg.length == 2) {
        if (!arg[0].equals("-v"))
            usage();
        validate = true;
        filename = arg[1];
    } else {
        usage();
    }

    // Create a new factory to create parsers that will
    // validate or not, according to the flag setting.
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(validate);

    // Create the XMLReader to be used to check for errors.
    XMLReader reader = null;
    try {
        SAXParser parser = spf.newSAXParser();
        reader = parser.getXMLReader();
    } catch (Exception e) {
        System.err.println(e);
        System.exit(1);
    }

    // Install an error handler in the reader.
    reader.setErrorHandler(new MyErrorHandler());
    // Use the XMLReader to parse the entire file.
    try {
        InputSource is = new InputSource(filename);
        reader.parse(is);
    } catch (SAXException e) {
        System.exit(1);
    } catch (IOException e) {
        System.err.println(e);
        System.exit(1);
    }
}

From source file:TrySAXHandler.java

public static void main(String args[]) throws Exception {
        File file = new File("y.xml");
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser parser = null;//from  www . j  av a 2s  .c  o m
        spf.setNamespaceAware(true);
        spf.setValidating(true);
        System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
        System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");

        parser = spf.newSAXParser();
        System.out.println("Parser object is: " + parser);

        MySAXHandler handler = new MySAXHandler();
        parser.parse(file, handler);
    }

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser parser = null;/* ww w  . ja  va2s  .c  o m*/
    spf.setNamespaceAware(true);
    spf.setValidating(true);
    try {
        spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        parser = spf.newSAXParser();
        System.out.println("Parser object is: " + parser);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }

    MySAXHandler handler = new MySAXHandler();
    parser.parse(new InputSource(new StringReader(xmlString)), handler);
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    SAXParser parser = null;/* w w  w. j a  v a  2  s  . c  o  m*/
    spf.setNamespaceAware(true);
    spf.setValidating(true);
    try {
        spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
        parser = spf.newSAXParser();
        System.out.println("Parser object is: " + parser);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    } catch (ParserConfigurationException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }

    MySAXHandler handler = new MySAXHandler();
    parser.parse(new InputSource(new StringReader(xmlString)), handler);
}

From source file:SAXDemo.java

/** The main method sets things up for parsing */
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
    // Create a JAXP "parser factory" for creating SAX parsers
    javax.xml.parsers.SAXParserFactory spf = SAXParserFactory.newInstance();

    // Configure the parser factory for the type of parsers we require
    spf.setValidating(false); // No validation required

    // Now use the parser factory to create a SAXParser object
    // Note that SAXParser is a JAXP class, not a SAX class
    javax.xml.parsers.SAXParser sp = spf.newSAXParser();

    // Create a SAX input source for the file argument
    org.xml.sax.InputSource input = new InputSource(new FileReader(args[0]));

    // Give the InputSource an absolute URL for the file, so that
    // it can resolve relative URLs in a <!DOCTYPE> declaration, e.g.
    input.setSystemId("file://" + new File(args[0]).getAbsolutePath());

    // Create an instance of this class; it defines all the handler methods
    SAXDemo handler = new SAXDemo();

    // Finally, tell the parser to parse the input and notify the handler
    sp.parse(input, handler);//ww  w  .  j  a va 2s.  c  o  m

    // Instead of using the SAXParser.parse() method, which is part of the
    // JAXP API, we could also use the SAX1 API directly. Note the
    // difference between the JAXP class javax.xml.parsers.SAXParser and
    // the SAX1 class org.xml.sax.Parser
    //
    // org.xml.sax.Parser parser = sp.getParser(); // Get the SAX parser
    // parser.setDocumentHandler(handler); // Set main handler
    // parser.setErrorHandler(handler); // Set error handler
    // parser.parse(input); // Parse!
}

From source file:Main.java

public static SAXParser getSAXParser() throws Exception {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setValidating(true);
    saxParserFactory.setNamespaceAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();
    saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    return saxParser;
}

From source file:Main.java

public static boolean validateWithDTDUsingSAX(String xml) throws Exception {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(true);/*from ww w.  jav  a 2s  .  com*/

    SAXParser parser = factory.newSAXParser();
    XMLReader reader = parser.getXMLReader();
    reader.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException e) throws SAXException {
            System.out.println("WARNING : " + e.getMessage()); // do nothing
        }

        public void error(SAXParseException e) throws SAXException {
            System.out.println("ERROR : " + e.getMessage());
            throw e;
        }

        public void fatalError(SAXParseException e) throws SAXException {
            System.out.println("FATAL : " + e.getMessage());
            throw e;
        }
    });
    reader.parse(new InputSource(xml));
    return true;
}

From source file:Main.java

public static SAXParser getParser() throws ParserConfigurationException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);//from   w w  w . ja  va2s  .  c  om
    factory.setValidating(false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    return factory.newSAXParser();
}