Java XML Format print(Node node)

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

Description

Print a DOM to standard out.

License

LGPL

Parameter

Parameter Description
node The root node

Declaration

public static void print(Node node) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 *                        Copyright Yumetech, Inc (c) 2006
 *                               Java Source
 *
 * This source is licensed under the GNU LGPL v2.1
 * Please read http://www.gnu.org/copyleft/lgpl.html for more information
 *
 * This software comes with the standard NO WARRANTY disclaimer for any
 * purpose. Use it at your own risk. If there's a problem you get to fix it.
 *
 ****************************************************************************/

import java.io.*;
import org.w3c.dom.*;

import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.DOMSource;

public class Main {
    private static final String IDENTITY_TRANSFORM = "<xsl:stylesheet version = '1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'><xsl:output method='xml' omit-xml-declaration='yes' indent='yes'/><xsl:template match='node()|@*'> <xsl:copy>   <xsl:apply-templates select='@*'/>   <xsl:apply-templates/>   </xsl:copy> </xsl:template></xsl:stylesheet>";

    /**//w w  w. j av a  2s  .  com
     * Print a DOM to standard out.
     *
     * @param node The root node
     */
    public static void print(Node node) {
        if (node == null) {
            System.out.println("DOMUtils.print: Null node");
            return;
        }

        try {
            DOMSource ds = new DOMSource(node);
            StreamResult result = new StreamResult(System.out);
            TransformerFactory transFact = TransformerFactory.newInstance();

            // Source xslSource = new StreamSource(new
            // StringBufferInputStream(IDENTITY_TRANSFORM));
            Source xslSource = new StreamSource(new StringReader(IDENTITY_TRANSFORM));

            Transformer trans = transFact.newTransformer(xslSource);

            trans.transform(ds, result);
            System.out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Print a DOM to specified stream.
     *
     * @param node The root node
     * @param out The stream to print to
     */
    public static void print(Node node, PrintStream out) {
        try {
            DOMSource ds = new DOMSource(node);
            StreamResult result = new StreamResult(out);
            TransformerFactory transFact = TransformerFactory.newInstance();

            // Source xslSource = new StreamSource(new
            // StringBufferInputStream(IDENTITY_TRANSFORM));
            Source xslSource = new StreamSource(new StringReader(IDENTITY_TRANSFORM));

            Transformer trans = transFact.newTransformer(xslSource);

            trans.transform(ds, result);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Print a DOM to specified stream.
     *
     * @param node The root node
     * @param out The writer to print to
     */
    public static void print(Node node, Writer out) {
        try {
            DOMSource ds = new DOMSource(node);
            StreamResult result = new StreamResult(out);
            TransformerFactory transFact = TransformerFactory.newInstance();

            // Source xslSource = new StreamSource(new
            // StringBufferInputStream(IDENTITY_TRANSFORM));
            Source xslSource = new StreamSource(new StringReader(IDENTITY_TRANSFORM));

            Transformer trans = transFact.newTransformer(xslSource);

            trans.transform(ds, result);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related

  1. prettyXml(String xml)
  2. prettyXmlPrint(Node xml, OutputStream out)
  3. print(Element elm, OutputStream out)
  4. print(File file)
  5. print(Node node)
  6. print(Node node, OutputStream out)
  7. print(Node node, String encoding)
  8. print(Node node, Writer out, boolean omitdecl)
  9. print(SOAPMessage msg, OutputStream out)