Example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace

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

Introduction

In this page you can find the example usage for javax.xml.parsers DocumentBuilderFactory setIgnoringElementContentWhitespace.

Prototype


public void setIgnoringElementContentWhitespace(boolean whitespace) 

Source Link

Document

Specifies that the parsers created by this factory must eliminate whitespace in element content (sometimes known loosely as 'ignorable whitespace') when parsing XML documents (see XML Rec 2.10).

Usage

From source file:tufts.vue.ds.XMLIngest.java

private static org.w3c.dom.Document parseXML(Object input, boolean validating) {
    try {/*from  w w w. ja v  a2 s  .  co m*/
        // Create a builder factory
        javax.xml.parsers.DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringElementContentWhitespace(true);
        factory.setIgnoringComments(true);
        //factory.setCoalescing(true);
        factory.setValidating(validating);

        // Create the builder and parse the file
        final org.w3c.dom.Document doc;
        if (input instanceof String) {
            doc = factory.newDocumentBuilder().parse(new File((String) input));
        } else if (input instanceof InputSource) {
            doc = factory.newDocumentBuilder().parse((InputSource) input);
        } else if (input instanceof InputStream) {
            //                 InputSource encoded = new InputSource();
            //                 encoded.setByteStream((InputStream)input);
            //                 encoded.setEncoding("ISO-8859-1"); // TODO: get from url stream
            //                 doc = factory.newDocumentBuilder().parse(encoded);
            //                 //doc = factory.newDocumentBuilder().parse(new InputStreamReader((InputStream) input, "ISO-8859-1"));
            doc = factory.newDocumentBuilder().parse((InputStream) input);
        } else
            throw new Error("Unhandled input type: " + Util.tags(input));
        return doc;
    } catch (Throwable t) {
        t.printStackTrace();
    }
    /*catch (SAXException e) {
    // A parsing error occurred; the xml input is not valid
    } catch (ParserConfigurationException e) {
    } catch (IOException e) {
    }
    */
    return null;
}