Example usage for javax.xml.parsers SAXParserFactory isValidating

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

Introduction

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

Prototype


public boolean isValidating() 

Source Link

Document

Indicates whether or not the factory is configured to produce parsers which validate the XML content during parse.

Usage

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
    System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");

    SAXParser parser = null;/* w w w  .j ava 2 s.  co m*/
    parser = spf.newSAXParser();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);/*from  ww w  . j  ava 2s  .co  m*/
    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 = spf.newSAXParser();
    System.out.println("Parser object is: " + parser);
}

From source file:MainClass.java

public static void main(String args[]) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);//from  ww w.  ja  v a 2  s. co m
    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:TrySAXHandler.java

public static void main(String args[]) throws Exception {
        File file = new File("y.xml");
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser parser = null;// ww w.  j  a v a  2s  .co 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:Main.java

public void process() {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);/*from   www  .j  av a  2 s  . c  o m*/
    spf.setValidating(true);
    System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
    System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");
    try {
        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);
    }
    try {
        parser.parse(new InputSource(new StringReader(getXMLData())), this);
    } catch (IOException e) {
        e.printStackTrace(System.err);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
    }
}

From source file:TrySAX.java

private void process(File file) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);/*  ww w  .  j a  v a  2s  .c  o  m*/
    spf.setValidating(true);
    System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
    System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");
    try {
        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);
    }
    System.out.println("\nStarting parsing of " + file + "\n");
    try {
        parser.parse(file, this);
    } catch (IOException e) {
        e.printStackTrace(System.err);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
    }
}

From source file:org.opennms.core.test.xml.XmlTest.java

protected void validateXmlString(final String xml) throws Exception {
    if (getSchemaFile() == null) {
        LOG.warn("skipping validation, schema file not set");
        return;/*from ww w  . j  a v a2  s  .c o  m*/
    }

    final SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
    final File schemaFile = new File(getSchemaFile());
    LOG.debug("Validating using schema file: {}", schemaFile);
    final Schema schema = schemaFactory.newSchema(schemaFile);

    final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setValidating(true);
    saxParserFactory.setNamespaceAware(true);
    saxParserFactory.setSchema(schema);

    assertTrue("make sure our SAX implementation can validate", saxParserFactory.isValidating());

    final Validator validator = schema.newValidator();
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(xml.getBytes());
    final Source source = new StreamSource(inputStream);

    validator.validate(source);
}