Example usage for javax.xml.parsers DocumentBuilderFactory setCoalescing

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

Introduction

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

Prototype


public void setCoalescing(boolean coalescing) 

Source Link

Document

Specifies that the parser produced by this code will convert CDATA nodes to Text nodes and append it to the adjacent (if any) text node.

Usage

From source file:Main.java

public static Document loadString(String domContent) throws Exception {
    javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);//  ww  w .jav  a2s.  c o  m
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(false);
    DocumentBuilder builder = factory.newDocumentBuilder();

    char[] chars = new char[domContent.length()];
    domContent.getChars(0, domContent.length(), chars, 0);
    InputSource is = new InputSource(new CharArrayReader(chars));
    return (builder.parse(is));
}

From source file:Main.java

/**
 * load a xml file from OS file system and interpret it into a Document no
 * charset limited/*from  w  w  w.  j a v a2 s . c  om*/
 * 
 * @param xmlfile
 * @return Document
 * @throws Exception
 */
public static Document load(String xmlfile) throws Exception {
    javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(xmlfile);
}

From source file:Main.java

/**
 * load a xml file from OS file system and interpret it into a Document no
 * charset limited/*from  w w  w.  j a va2 s.  c om*/
 * 
 * @param xmlfile
 *            String
 * @return Document
 * @throws Exception
 */
public static Document load(File xmlfile) throws Exception {
    javax.xml.parsers.DocumentBuilderFactory factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(true);
    DocumentBuilder builder = factory.newDocumentBuilder();

    return builder.parse(xmlfile);
}

From source file:Main.java

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

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    factory.setValidating(false);/*from   w  ww .j  av a2  s. c o m*/

    factory.setNamespaceAware(false);

    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

public static Document blankDocument(String paramString) throws Exception {
    DocumentBuilderFactory localDocumentBuilderFactory = DocumentBuilderFactory.newInstance();
    localDocumentBuilderFactory.setIgnoringComments(false);
    localDocumentBuilderFactory.setIgnoringElementContentWhitespace(false);
    localDocumentBuilderFactory.setValidating(false);
    localDocumentBuilderFactory.setCoalescing(false);
    DocumentBuilder localDocumentBuilder = localDocumentBuilderFactory.newDocumentBuilder();
    Document localDocument = localDocumentBuilder.newDocument();
    Element localElement = localDocument.createElement(paramString);
    localDocument.appendChild(localElement);
    return localDocument;
}

From source file:Main.java

public static Document createDocument() {
    try {//  w  ww.j ava  2 s . c om
        // 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().newDocument();
    } catch (ParserConfigurationException errParser) {
        throw new RuntimeException("Error getting XML parser", errParser);
    }
}

From source file:Main.java

/**
 * Creates a new {@link DocumentBuilder} object.
 *///from  w w  w  . j  a va  2  s  .  c om
private static DocumentBuilder newDocumentBuilder() throws ParserConfigurationException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
    factory.setNamespaceAware(true);
    factory.setValidating(false);
    factory.setIgnoringComments(true);
    factory.setExpandEntityReferences(false);
    factory.setCoalescing(false);

    factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
    factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
    factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);

    return factory.newDocumentBuilder();
}

From source file:Main.java

public static Document parseStreamToXML(InputStream in) {
    try {/* w w w  .j av  a  2s. co  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 {//from  w  w  w .  j  a  v a 2s  . c  om
        // 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: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  a2  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());
}