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 DocumentBuilder getDOMBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*from   w  w w.j a  v  a2s. c  o  m*/
    dbf.setValidating(false);
    DocumentBuilder builder = dbf.newDocumentBuilder();
    return builder;
}

From source file:Main.java

public static Element loadXml(final InputStream inStream) throws IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);

    Document doc;//from www  .  j a va2  s  .c  o m
    try {
        doc = factory.newDocumentBuilder().parse(inStream);
    } catch (SAXException e) {
        throw new IOException(e.getLocalizedMessage(), e);
    } catch (ParserConfigurationException e) {
        throw new IOException(e.getLocalizedMessage(), e);
    }

    return doc.getDocumentElement();
}

From source file:Main.java

public static Document openXmlFile(File file, boolean validating) {
    try {/*from   ww w. jav  a  2 s . c  om*/
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validating);

        // Create the builder and parse the file
        Document doc = factory.newDocumentBuilder().parse(file);
        return doc;
    } catch (SAXException e) {
        // A parsing error occurred; the xml input is not valid
    } catch (ParserConfigurationException ex) {
        System.out.println(ex.getMessage());
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }

    return null;
}

From source file:Main.java

/** Creates a schema-aware XML parser */
private static DocumentBuilder createSchemaAwareParser(URL schemaURL) {
    DocumentBuilderFactory dbf = createNamespaceAwareDocumentBuilderFactory();
    dbf.setValidating(true);
    dbf.setAttribute(ATTRIBUTE_SCHEMA_LANGUAGE, SCHEMA_URL);
    dbf.setAttribute(ATTRIBUTE_SCHEMA_SOURCE, schemaURL.toString());

    try {//ww  w. j  a  v  a2  s  .co m
        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IllegalStateException("No document builder found, probably Java is misconfigured!", e);
    }
}

From source file:Main.java

/**
 * Liefert das {@link Document} aus der {@link InputSource}.
 * /*from  w w w.j a v a  2s.  c  om*/
 * @param inputSource {@link InputSource}
 * @return {@link Document}
 * @throws Exception Falls was schief geht.
 */
public static Document getDocument(final InputSource inputSource) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);

    Document document = factory.newDocumentBuilder().parse(inputSource);

    return document;
}

From source file:Main.java

public static Document parseXmlFile(String filename, boolean validating) {
    try {/*w w w  .  j av a2  s .  c o  m*/
        // Create a builder factory
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(validating);

        // Create the builder and parse the file
        Document doc = factory.newDocumentBuilder().parse(new File(filename));
        return doc;
    } catch (ParserConfigurationException e) {
        System.out.println("-----------------------------------------------");
        System.out.println("Error ParserConfiguration");
        System.out.println("-----------------------------------------------");
    } catch (SAXException e) {
        System.out.println("-----------------------------------------------");
        System.out.println("Error XML input is not valid");
        System.out.println("-----------------------------------------------");
    } catch (IOException e) {
        System.out.println("-----------------------------------------------");
        System.out.println("Error IO exception");
        System.out.println("-----------------------------------------------");
    }
    return null;
}

From source file:Main.java

public static Document makeEmptyXmlDocument(String rootElementName) {
    Document document = null;//from   w  ww. ja  v a 2s.co  m
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(true);
    // factory.setNamespaceAware(true);
    try {
        DocumentBuilder builder = factory.newDocumentBuilder();

        document = builder.newDocument();
    } catch (Exception e) {
        //Debug.logError(e, module);
    }

    if (document == null)
        return null;

    if (rootElementName != null) {
        Element rootElement = document.createElement(rootElementName);
        document.appendChild(rootElement);
    }

    return document;
}

From source file:Utils.java

public static Document readXml(Reader is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);//from w w w .  j av  a2  s.c o  m
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

    DocumentBuilder db = null;
    db = dbf.newDocumentBuilder();
    db.setEntityResolver(new NullResolver());

    // db.setErrorHandler( new MyErrorHandler());
    InputSource ips = new InputSource(is);
    return db.parse(ips);
}

From source file:Main.java

/**
 * Get a DocumentBuilder that is namespace aware.
 * @return a namespace-aware DocumentBuilder.
 *///from w w  w  .  j  a v a 2  s. c o m
public static DocumentBuilder getDocumentBuilder() throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(false);
    dbf.setXIncludeAware(false);
    dbf.setExpandEntityReferences(true);
    dbf.setCoalescing(false);

    //dbf.setFeature("http://xml.org/sax/features/namespaces", false);
    //dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    dbf.setFeature("http://xml.org/sax/features/validation", false);
    dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    return dbf.newDocumentBuilder();
}

From source file:Main.java

private static DocumentBuilderFactory createDocumentBuilderFactory() {
    DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
    // Assume default processing is too strict
    f.setValidating(false);
    f.setIgnoringComments(true);//from  www. jav a 2 s  .c  om
    f.setNamespaceAware(true);

    return f;
}