Example usage for javax.xml.parsers DocumentBuilderFactory setValidating

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

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory 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:Main.java

public static boolean isXMLWellFormed(String xmlUrl)
        throws IOException, SAXException, ParserConfigurationException {

    boolean isXMLValid = false;

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);
    factory.setNamespaceAware(true);/*  w  w  w.j  a v a2  s. c om*/
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = (Document) builder.parse(new InputSource(xmlUrl));
    isXMLValid = true;

    return isXMLValid;
}

From source file:Main.java

public static Document parseXmlFile(File xmlFile)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);

    Document doc = factory.newDocumentBuilder().parse(xmlFile);
    return doc;/*  ww w. j a  v  a2s .  c  o m*/
}

From source file:Main.java

private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    if (db == null) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setValidating(false);
        dbf.setIgnoringComments(true);/*from www.ja  v a2 s .  com*/
        dbf.setFeature("http://xml.org/sax/features/namespaces", false);
        dbf.setFeature("http://xml.org/sax/features/validation", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

        db = dbf.newDocumentBuilder();
    }
    return db;
}

From source file:Main.java

public static boolean validateWithDTDUsingDOM(String xml) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(true);//from  w w w . ja v  a  2  s.  c o  m

    DocumentBuilder builder = factory.newDocumentBuilder();

    builder.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;
        }
    });
    builder.parse(new InputSource(xml));
    return true;
}

From source file:Main.java

public static Document CreateDocument(Path xml, Path xsd)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(true);/*from  w ww .j a  va  2s .  c  o  m*/
    factory.setIgnoringComments(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    if (xsd != null)
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xsd.toString());
    return factory.newDocumentBuilder().parse(xml.toString());
}

From source file:Main.java

/**
 * Loads an XML document from the given file.
 * @param file XML file to load.//from w ww. ja  v  a  2s . c om
 * @param isValidating true: XML doc is validated against the DTD specifed
 *   within the XML document, false: otherwise.
 *   This does not work for XML Schemata! See {@link #load(File, Schema)}
 *   for that.
 * @param isNamespaceAware true: XML doc is namespace aware, false: otherwise.
 * @return Returns an XML document.
 * @throws IOException Throws an exception if the file couldn't be read.
 */
static public Document load(File file, boolean isValidating, boolean isNamespaceAware) throws IOException {
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(isValidating);
        factory.setNamespaceAware(isNamespaceAware);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return (builder.parse(file));
    } catch (ParserConfigurationException pce) {
        throw new IOException("Parser configuration error: " + pce);
    } catch (SAXException se) {
        throw new IOException("XML parsing error: " + se);
    }
}

From source file:Main.java

public static DocumentBuilderFactory createNonValidatingDocumentBuilderFactory()
        throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);
    factory.setNamespaceAware(true);/*from  w w  w  .jav  a 2  s.  co  m*/
    factory.setFeature("http://xml.org/sax/features/namespaces", false);
    factory.setFeature("http://xml.org/sax/features/validation", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    return factory;
}

From source file:Main.java

public static Document parseXml(InputStream input) {
    try {//from w w w  .  ja v  a2s .  c o m
        DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
        fac.setValidating(false);

        DocumentBuilder docBuilder = fac.newDocumentBuilder();
        Document doc = docBuilder.parse(input);

        return doc;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:Main.java

/**
 * Builds new DocumentBuilder//from  w  w w  . j  ava2s  .co  m
 * 
 * @return DocumentBuilder
 * @throws CitationStyleManagerException
 */
public static DocumentBuilder createDocumentBuilder() {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setValidating(false);
    dbf.setIgnoringComments(true);
    dbf.setNamespaceAware(false);

    try {
        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Failed to create a document builder factory", e);
    }
}

From source file:Main.java

public static Document CreateDocument(Path xml, Path xsd)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);
    factory.setNamespaceAware(true);/*from  w  w  w .jav  a  2s.  c  o  m*/
    factory.setIgnoringComments(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    if (xsd != null) {
        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xsd.toString());
    }
    return factory.newDocumentBuilder().parse(xml.toString());
}