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

/**
 * Creates a non-validating DocumentBuilder with the following settings:
 * <ul>/*from   w w  w. j  av a  2s .c  om*/
 * <li>Validating: false
 * <li>NamespaceAware: true
 * </ul>
 */
public static DocumentBuilder createNonValidatingDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    dbFactory.setValidating(false); // if true, then need to also set an org.xml.sax.ErrorHandler on DocumentBuilder
    dbFactory.setNamespaceAware(true);

    DocumentBuilder result = dbFactory.newDocumentBuilder();

    return result;
}

From source file:Main.java

public static org.w3c.dom.Document configureJDomDocFromResource(String fileName) {
    org.w3c.dom.Document doc = null;
    InputStream in = ClassLoader.getSystemResourceAsStream(fileName);

    if (in != null) {
        try {/*from w ww  .  j a  va  2s  .c o  m*/
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setNamespaceAware(false);
            doc = factory.newDocumentBuilder().parse(in);
        } catch (Exception e) {
            //                Debug.error(e);
        }
    } else {
        //            Debug.error("could not find file " + fileName);
    }

    return doc;
}

From source file:Main.java

public static Document read(String filename) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);
    DocumentBuilder builder = null;
    Document document = null;/*  w ww .j  a  v a 2 s .  c o m*/
    try {
        builder = factory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId)
                    throws SAXException, IOException {
                return new InputSource(new StringReader(""));
            }
        });
        document = builder.parse(new File(filename));
        document.getDocumentElement().normalize();
    } catch (Exception e) {
    }
    return document;
}

From source file:Main.java

public static String getDefaultNamespaceUri(URL xmlResource, String rootElementName)
        throws IOException, ParserConfigurationException, SAXException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(xmlResource.openStream());
    NodeList nodes = document.getElementsByTagName(rootElementName);
    if (nodes == null || nodes.getLength() == 0) {
        throw new IllegalArgumentException("Root element \"" + rootElementName + "\" not found in xml \""
                + xmlResource.toExternalForm() + "\".");
    }/*from www. j a  v a2s  .  com*/
    for (int i = 0; i < nodes.getLength(); i++) {
        Node node = nodes.item(i);
        Node xmlns = node.getAttributes().getNamedItem("xmlns");
        if (xmlns != null) {
            String value = xmlns.getNodeValue();
            return value.substring(value.indexOf("=") + 1);
        }
    }
    return null;
}

From source file:Main.java

public static Node openFile(String fileName) {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);
    factory.setNamespaceAware(false);/*from   w  w  w  .j  a  v a2 s .  c  o m*/
    DocumentBuilder builder;
    Document document = null;
    try {
        builder = factory.newDocumentBuilder();
        document = builder.parse(new File(fileName));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return document;
}

From source file:Main.java

public static Document loadFromStream(InputStream inputStream) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(false);
    factory.setNamespaceAware(false);/* w w  w .  j  a v  a  2s.  c om*/

    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setEntityResolver(new EntityResolver() {

        @Override
        public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
            //System.out.println("Ignoring " + publicId + ", " + systemId);
            return new InputSource(new StringReader(""));
        }
    });

    //System.out.println(StringUtil.toString(inputStream));

    Document doc = builder.parse(inputStream);
    return doc;
}

From source file:Main.java

public static Document parse(String xmlStr) throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);

    factory.setNamespaceAware(false);//from   w  w  w  .ja  va2  s. c  om

    factory.setIgnoringComments(true);

    factory.setIgnoringElementContentWhitespace(false);

    factory.setCoalescing(false);

    factory.setExpandEntityReferences(true);

    DocumentBuilder builder = factory.newDocumentBuilder();

    ByteArrayInputStream inputStream = new ByteArrayInputStream(xmlStr.getBytes());

    return builder.parse(inputStream);

}

From source file:Main.java

/**
 * /* w  ww  . j a  v  a  2  s  . c o  m*/
 */
public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setIgnoringComments(true);
    factory.setCoalescing(true);
    factory.setNamespaceAware(false);

    return factory.newDocumentBuilder();
}

From source file:Main.java

private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilder documentBuilder;

    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

    documentBuilderFactory.setValidating(false); // dtd isn't available; would be nice to attempt to validate
    documentBuilderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilder = documentBuilderFactory.newDocumentBuilder();

    return documentBuilder;
}

From source file:Main.java

/** Read XML as DOM.
 *//* w  w  w . ja v a 2 s .  co  m*/
public static Document readXml(InputStream is) throws SAXException, IOException, ParserConfigurationException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    dbf.setValidating(false);
    dbf.setIgnoringComments(false);
    dbf.setIgnoringElementContentWhitespace(true);
    //dbf.setCoalescing(true);
    //dbf.setExpandEntityReferences(true);

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

    // db.setErrorHandler( new MyErrorHandler());

    Document doc = db.parse(is);
    return doc;
}