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 String toNormalizedXML(InputStream is)
        throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from  w  w  w.  j a va 2  s  .co m
    dbf.setCoalescing(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.parse(is);
    document.normalizeDocument();
    document.getDocumentElement().setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", //$NON-NLS-1$
            "xsi:schemaLocation", //$NON-NLS-1$
            "http://abc4trust.eu/wp2/abcschemav1.0 ../../../../../../../../../abc4trust-xml/src/main/resources/xsd/schema.xsd"); //$NON-NLS-1$
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    String xml = serializer.writeToString(document);

    return trim(xml);
}

From source file:Main.java

public static boolean compareXmls(InputStream xml1, InputStream xml2)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from  w  ww. java  2s  .c  o m
    dbf.setCoalescing(true);
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setIgnoringComments(true);
    DocumentBuilder db = dbf.newDocumentBuilder();

    Document doc1 = db.parse(xml1);
    doc1.normalizeDocument();

    Document doc2 = db.parse(xml2);
    doc2.normalizeDocument();

    return doc2.isEqualNode(doc1);
}

From source file:Main.java

static Document getLoadingDoc(InputStream in) throws SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setIgnoringElementContentWhitespace(true);
    dbf.setValidating(false);/*  ww w.j  av a 2 s. c  o m*/
    dbf.setCoalescing(true);
    dbf.setIgnoringComments(true);
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(in);
        return db.parse(is);
    } catch (ParserConfigurationException x) {
        throw new Error(x);
    }
}

From source file:Main.java

/**
 * Returns a default DocumentBuilder instance or throws an
 * ExceptionInInitializerError if it can't be created.
 *
 * @return a default DocumentBuilder instance.
 *//*www . j  a v a2s.c om*/
public static DocumentBuilder getDocumentBuilder() throws Exception {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setIgnoringComments(true);
        dbf.setCoalescing(true);
        dbf.setIgnoringElementContentWhitespace(true);
        dbf.setValidating(false);
        return dbf.newDocumentBuilder();
    } catch (Exception exc) {
        throw new Exception(exc.getMessage());
    }
}

From source file:Main.java

public static Document load(File fileName) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);//w  ww  .  j  a va2 s  .  c om
    factory.setIgnoringElementContentWhitespace(true);
    factory.setValidating(false);
    factory.setCoalescing(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(fileName);
}

From source file:Main.java

public static Document loadString(String paramString) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);/*from   ww w. ja  v  a2 s .  co m*/
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(false);
    DocumentBuilder builder = factory.newDocumentBuilder();
    char[] arrayOfChar = new char[paramString.length()];
    paramString.getChars(0, paramString.length(), arrayOfChar, 0);
    InputSource input = new InputSource(new CharArrayReader(arrayOfChar));
    return builder.parse(input);
}

From source file:Main.java

public static Document load(String path) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);//from w w w .  j  av a  2s  .com
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(path);
}

From source file:Main.java

public static Document load(File file) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setIgnoringComments(false);//  w w w  .  ja v a  2s .com
    factory.setIgnoringElementContentWhitespace(false);
    factory.setValidating(false);
    factory.setCoalescing(true);
    DocumentBuilder builder = factory.newDocumentBuilder();
    return builder.parse(file);
}

From source file:Main.java

/**
 * Get a DocumentBuilder that is namespace aware.
 * @return a namespace-aware DocumentBuilder.
 *///from  w w  w .ja  v a2s.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

/**
 * // w ww  .  ja v a  2s .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();
}