Java XML DOM Print prettyPrintDOM(Node node)

Here you can find the source of prettyPrintDOM(Node node)

Description

Serializes the specified node to stdout.

License

BSD License

Parameter

Parameter Description
node the node to serialize

Declaration

public static void prettyPrintDOM(Node node) 

Method Source Code


//package com.java2s;
/*//from  ww  w.jav a2s . com
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

import org.w3c.dom.*;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;

public class Main {
    /**
     * Serializes the specified node to stdout.
     *
     * @param node the node to serialize
     */
    public static void prettyPrintDOM(Node node) {
        try {
            System.out.println();
            prettyPrintDOM(node, System.out);
            System.out.println("\n");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Serializes the specified node to the given stream. Serialization is achieved by an identity transform.
     *
     * @param node   the node to serialize
     * @param stream the stream to serialize to.
     * @throws TransformerException if any error ccurred during the identity transform.
     */
    public static void prettyPrintDOM(Node node, OutputStream stream) throws TransformerException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(new DOMSource(node), new StreamResult(stream));
    }

    public static void prettyPrintDOM(Node node, Node output) throws TransformerException {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.transform(new DOMSource(node), new DOMResult(node));
    }
}

Related

  1. prettyPrintDOM(Node node, OutputStream stream)
  2. prettyPrintDOMAsHTML(Node node, OutputStream stream)
  3. printDom(Node dom, OutputStream os)
  4. printDOM(Node node, OutputStream out, String encoding)