Example usage for org.w3c.dom Document normalize

List of usage examples for org.w3c.dom Document normalize

Introduction

In this page you can find the example usage for org.w3c.dom Document normalize.

Prototype

public void normalize();

Source Link

Document

Puts all Text nodes in the full depth of the sub-tree underneath this Node, including attribute nodes, into a "normal" form where only structure (e.g., elements, comments, processing instructions, CDATA sections, and entity references) separates Text nodes, i.e., there are neither adjacent Text nodes nor empty Text nodes.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    factory.setValidating(true);//from w  w  w  .  j  a v  a2 s  .  c o m
    factory.setExpandEntityReferences(false);

    Document doc = factory.newDocumentBuilder().parse(new File("filename"));

    removeAll(doc, Node.ELEMENT_NODE, "junk");

    removeAll(doc, Node.COMMENT_NODE, null);

    doc.normalize();

}

From source file:Main.java

/**
 * Stores an XMl document into a file.//w  w w.j a  v a  2  s .  c  o  m
 *
 * @param doc xml document to be stored
 * @param location absolute path where to write down the document
 * @throws TransformerException If an unrecoverable error occurs during the
 * course of the transformation.
 */
public static void save(Document doc, String location) throws TransformerException {
    doc.normalize();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    DOMSource source = new DOMSource(doc);
    StreamResult streamResult = new StreamResult(new File(location));
    transformer.transform(source, streamResult);
}

From source file:Main.java

/**
 * Coalesce adjacent text nodes in the given document. CDATA sections are preserved. This method
 * is useful to compare output from two non coalescing parsers.
 * /* w  w  w  .  j a  va  2s .  c  o m*/
 * @param document
 *            the document to process
 */
public static void coalesceTextNodes(Document document) {
    document.getDomConfig().setParameter("cdata-sections", true);
    document.normalize();
}

From source file:Main.java

public static void DocumentToStream(final Document doc, OutputStream stream) {
    Result l_s = new StreamResult(stream);

    doc.normalize();

    try {//from www . ja  va  2s . co  m
        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s);
        stream.close();
    } catch (Exception e) {
        System.err.println(e);

        return;
    }
}

From source file:Main.java

/**
 * As a workaround for {@code javax.xml.transform.Transformer} not being able
 * to pretty print XML. This method prepares DOM {@code Document} for the transformer
 * to be pretty printed, i.e. providing proper indentations for enclosed tags.
 *
 * @param doc - DOM document to be pretty printed
 * @param ident - custom indentation as a string of white spaces
 *//*from   w ww .j  a  va2 s .c  om*/
public static void prettyFormat(Document doc, String ident) {
    doc.normalize();
    Element documentElement = doc.getDocumentElement();
    if (documentElement != null) {
        prettyFormat(documentElement, "", ident); //$NON-NLS-1$
    }
}

From source file:Main.java

/**
 *
 *
 * @param in .../*w w  w  . j  ava 2s. c  o m*/
 *
 * @return ...
 *
 * @throws RuntimeException ...
 */
public static Document parse(InputStream in) throws RuntimeException {
    DocumentBuilder d = getDocumentBuilder();

    try {
        Document doc = d.parse(in);

        doc.normalize();

        return doc;
    } catch (SAXException e) {
        throw new RuntimeException("Bad xml-code: " + e.toString());
    } catch (IOException f) {
        throw new RuntimeException("Could not read Xml: " + f.toString());
    }
}

From source file:Main.java

/**
 *
 *
 * @param in .../*from   ww w .  j  a  v a2  s.co  m*/
 *
 * @return ...
 *
 * @throws RuntimeException ...
 */
public static Document parse(InputSource in) throws RuntimeException {
    DocumentBuilder d = getDocumentBuilder();

    try {
        Document doc = d.parse(in);

        doc.normalize();

        return doc;
    } catch (SAXException e) {
        throw new RuntimeException("Bad xml-code: " + e.toString());
    } catch (IOException f) {
        throw new RuntimeException("Could not read Xml: " + f.toString());
    }
}

From source file:Main.java

private static Document loadXML(String path) throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.parse(new File(path));
    document.normalize();

    return document;
}

From source file:Main.java

public static String DocumentToString(final Document doc) {
    try {//from   w  w  w . jav a2s .  c o m
        StringWriter writer = new StringWriter();
        Result l_s = new StreamResult(writer);

        doc.normalize();

        TransformerFactory.newInstance().newTransformer().transform(new DOMSource(doc), l_s);

        return writer.toString();
    } catch (Exception e) {
        System.err.println(e);

        return null;
    }
}

From source file:Main.java

public static Document load(String filename) {
    Document document = null;
    try {//from   ww w  .j  a v a  2  s  .com
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        document = builder.parse(new File(filename));
        document.normalize();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return document;
}