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:Main.java

public static Document parseStreamToXML(InputStream in) {
    try {//from w w w.  j  ava  2 s .  c o m
        // Use JAXP to create a document builder
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        builderFactory.setExpandEntityReferences(false);
        builderFactory.setValidating(false);
        builderFactory.setNamespaceAware(true);
        builderFactory.setIgnoringComments(true);
        builderFactory.setCoalescing(true);
        builderFactory.setIgnoringElementContentWhitespace(true);

        return builderFactory.newDocumentBuilder().parse(in);
    } catch (ParserConfigurationException errParser) {
        throw new RuntimeException("Error getting XML parser", errParser);
    } catch (SAXException errSax) {
        throw new RuntimeException("Error parsing XML files", errSax);
    } catch (IOException errIO) {
        throw new RuntimeException("Error parsing XML files", errIO);
    }
}

From source file:Main.java

public static Document parseStreamToXML(Reader in) {
    try {//ww  w .  ja  v a  2s. c o  m
        // Use JAXP to create a document builder
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();

        builderFactory.setExpandEntityReferences(false);
        builderFactory.setValidating(false);
        builderFactory.setNamespaceAware(true);
        builderFactory.setIgnoringComments(true);
        builderFactory.setCoalescing(true);
        builderFactory.setIgnoringElementContentWhitespace(true);

        return builderFactory.newDocumentBuilder().parse(new InputSource(in));
    } catch (ParserConfigurationException errParser) {
        throw new RuntimeException("Error getting XML parser", errParser);
    } catch (SAXException errSax) {
        throw new RuntimeException("Error parsing XML files", errSax);
    } catch (IOException errIO) {
        throw new RuntimeException("Error parsing XML files", errIO);
    }
}

From source file:kenh.xscript.ScriptUtils.java

/**
 * Get xScript instance./*from   ww  w.java 2s .  co m*/
 * @param url
 * @param env
 * @return
 * @throws UnsupportedScriptException
 */
public static final Element getInstance(URL url, Environment env) throws UnsupportedScriptException {

    if (url == null)
        return null;

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);
        factory.setNamespaceAware(true);
        factory.setIgnoringElementContentWhitespace(true);

        InputStream in = url.openStream();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(in);
        return getInstance(doc, env);
    } catch (UnsupportedScriptException e) {
        throw e;
    } catch (Exception e) {
        throw new UnsupportedScriptException(null, e);
    }
}

From source file:kenh.xscript.ScriptUtils.java

/**
 * Get xScript instance./* w  w  w  .j  av  a2  s. c  o  m*/
 * @param file
 * @param env
 * @return
 * @throws UnsupportedScriptException
 */
public static final Element getInstance(File file, Environment env) throws UnsupportedScriptException {

    if (file == null || !file.exists())
        return null;

    if (env == null)
        env = new Environment();

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setIgnoringComments(true);
        factory.setNamespaceAware(true);
        factory.setIgnoringElementContentWhitespace(true);

        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(new FileInputStream(file));
        String home = file.getCanonicalFile().getParent();
        if (env != null) {
            if (!env.containsVariable(Constant.VARIABLE_HOME))
                env.setPublicVariable(Constant.VARIABLE_HOME, home, false);
        }
        return getInstance(doc, env);
    } catch (UnsupportedScriptException e) {
        throw e;
    } catch (Exception e) {
        throw new UnsupportedScriptException(null, e);
    }
}

From source file:dk.dbc.rawrepo.oai.OAIWorker.java

private static DocumentBuilder makeDocumentBuilder() {
    synchronized (DocumentBuilderFactory.class) {
        try {/*from w w  w  .  ja  va  2 s .  co  m*/
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            dbf.setNamespaceAware(true);
            dbf.setIgnoringComments(true);
            dbf.setIgnoringElementContentWhitespace(true);
            DocumentBuilder db = dbf.newDocumentBuilder();
            db.setEntityResolver(new NullResolver());
            return db;
        } catch (ParserConfigurationException ex) {
            throw new RuntimeException(ex);
        }
    }
}

From source file:com.vmware.qe.framework.datadriven.utils.XMLUtil.java

/**
 * Validates an XML file with its schema.
 * // ww  w . j  a  va  2  s .c  om
 * @param xmlFileStream - the inputStream of the xml content
 * @param xsdFileStream - the inputStream of the xsd content
 * @throws Exception
 */
public static void validateXML(InputStream xmlFileStream, InputStream xsdFileStream) throws Exception {
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    documentBuilderFactory.setIgnoringElementContentWhitespace(true);
    documentBuilderFactory.setValidating(false);
    DocumentBuilder parser = documentBuilderFactory.newDocumentBuilder();
    Document document = parser.parse(xmlFileStream);
    validateDocument(document, xsdFileStream);
}

From source file:com.vmware.qe.framework.datadriven.utils.XMLUtil.java

/**
 * Creates the parser instance based on dom parser factory Validates the xml file against the
 * XSD schema Return the documentElement of the xml document if validations succeeded
 * //from   ww  w  .  j  av a  2  s. c o m
 * @param xmlFile - XML file to be parsed
 * @param xmlSchema - XSD that XML file should be validated against
 * @return documentElement of the XML file
 */
public static Document getXmlDocumentElement(String xmlFile, String xmlSchema) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setCoalescing(true);
    factory.setNamespaceAware(false);
    factory.setIgnoringElementContentWhitespace(true);
    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",
            XMLUtil.class.getResource(xmlSchema).getFile());
    DocumentBuilder builder = factory.newDocumentBuilder();
    builder.setErrorHandler(new org.xml.sax.helpers.DefaultHandler());
    // Parse the document from the classpath.
    URL xmlFileUri = XMLUtil.class.getResource(xmlFile);
    if (null == xmlFileUri) {
        log.error("Unable to find file on classpath: " + xmlFile);
        return null;
    }
    return builder.parse(xmlFileUri.getFile());
}

From source file:com.bcmcgroup.flare.client.ClientUtil.java

/**
 * Constructs a DocumentBuilder object for XML documents
 *
 * @return DocumentBuilder object with the proper initializations
 *//*from  w  ww.  j av  a 2s .c  om*/
public static DocumentBuilder generateDocumentBuilder() {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setIgnoringComments(true);
        dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
        dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
        dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
        return dbf.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        logger.error("ParserConfigurationException when attempting to generate a document builder.");
    }
    return null;
}

From source file:com.vmware.qe.framework.datadriven.utils.XMLUtil.java

/**
 * Creates the parser instance based on DOM parser factory, using the contents of a XML file.
 * Validation is not done. Return the documentElement of the XML document
 * //from   ww  w.  j a  v a 2  s .  co  m
 * @param xmlFile - XML file to be parsed
 * @return documentElement of the XML file
 */
public static Document getXmlDocumentElement(String xmlFileContents) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    Reader reader = new StringReader(xmlFileContents);
    InputSource inputSource = new InputSource(reader);
    factory.setCoalescing(true);
    factory.setNamespaceAware(false);
    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(inputSource);
}

From source file:com.vmware.identity.SharedUtils.java

/**
 * Read XML as DOM.// w w  w  .  j  a v  a 2s  . 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.setNamespaceAware(true);
    // dbf.setCoalescing(true);
    // dbf.setExpandEntityReferences(true);

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

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

    return db.parse(is);
}