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 Document validate(File xml, File xmlSchema)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setNamespaceAware(true);/*from  w w  w  .  j av  a2s  . c  o m*/
    factory.setValidating(true);
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource", xmlSchema);
    DocumentBuilder documentBuilder = factory.newDocumentBuilder();
    documentBuilder.setErrorHandler(new ErrorHandler() {
        public void warning(SAXParseException ex) throws SAXException {
            throw ex;
        }

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

        public void fatalError(SAXParseException ex) throws SAXException {
            throw ex;
        }
    });
    return documentBuilder.parse(xml);
}

From source file:Main.java

static Document getLoadingDoc(InputStream in) throws SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setValidating(false);
    dbf.setCoalescing(true);//from  w w  w . j  a  v  a2 s .c om
    dbf.setIgnoringComments(true);
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(in);
        return db.parse(is);
    } catch (ParserConfigurationException x) {
        throw new Error(x);
    }
}

From source file:Main.java

private static DocumentBuilder getDocumentBuilder() {
    try {// w ww . j a  va  2s .  c  o m
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(false);
        factory.setValidating(false);
        factory.setXIncludeAware(false);
        return factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setValidating(false);
    return documentBuilderFactory.newDocumentBuilder();
}

From source file:Main.java

/**
 * Obtains a new instance of a DOM Document object
 * //from  ww  w . j a  v a  2  s  . co  m
 * @return a new instance of a DOM Document object
 * @exception Exception
 *                if an error occurs while constructing a new document
 */
public static Document newDocument() throws ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    dbFactory.setValidating(validating);
    return dbFactory.newDocumentBuilder().newDocument();
}

From source file:Main.java

public static Document stringToDocument(String xml) {
    Document doc = null;//from  w ww. j a  va2 s.  co  m
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    // Turn on validation, and turn off namespaces
    factory.setValidating(false);
    factory.setNamespaceAware(false);

    try {
        DocumentBuilder builder = factory.newDocumentBuilder();
        doc = builder.parse(new InputSource(new StringReader(xml)));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return doc;
}

From source file:Main.java

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

    // ist das langsammer: factory.setValidating(true);
    factory.setValidating(false);
    factory.setNamespaceAware(false);/*  ww w  .ja v  a 2  s .c  om*/

    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder;
}

From source file:Main.java

public static Document getXMLDocument(String xmlContent) {

    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    Document xmlDocument = null;//w w w  . j  av a  2  s. co  m
    builderFactory.setValidating(false);
    builderFactory.setNamespaceAware(true);
    try {
        DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
        try {
            xmlDocument = documentBuilder.parse(new InputSource(new StringReader(xmlContent)));
        } catch (SAXException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    } catch (ParserConfigurationException e) {
        throw new RuntimeException(e);
    }
    return xmlDocument;

}

From source file:XMLUtil.java

public static Document parse(InputSource input, boolean validate, boolean namespaceAware,
        ErrorHandler errorHandler, EntityResolver entityResolver) throws IOException, SAXException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validate);
    factory.setNamespaceAware(namespaceAware);
    DocumentBuilder builder = null;
    try {//from   w  w w  .  jav  a 2s .c om
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException ex) {
        throw new SAXException(ex);
    }

    if (errorHandler != null) {
        builder.setErrorHandler(errorHandler);
    }

    if (entityResolver != null) {
        builder.setEntityResolver(entityResolver);
    }

    assert input != null : "InputSource cannot be null";

    return builder.parse(input);
}

From source file:Main.java

public static Document parseXmlFile(URI uri, boolean validating)
        throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(validating);
    Document doc = factory.newDocumentBuilder().parse(uri.toString());
    return doc;/*from   ww  w  . j a va  2  s. co m*/
}