Java XML Format printNode(Node n)

Here you can find the source of printNode(Node n)

Description

Print the specified XML DOM node to System.out

License

RPL License

Parameter

Parameter Description
n the node to print

Exception

Parameter Description
Exception an exception

Declaration

public static final void printNode(Node n) throws Exception 

Method Source Code

//package com.java2s;
/*//from w w w.  ja v  a  2s. co m
 * Unless explicitly acquired and licensed from Licensor under another license, the contents of
 * this file are subject to the Reciprocal Public License ("RPL") Version 1.5, or subsequent
 * versions as allowed by the RPL, and You may not copy or use this file in either source code
 * or executable form, except in compliance with the terms and conditions of the RPL
 *
 * All software distributed under the RPL is provided strictly on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, AND LICENSOR HEREBY DISCLAIMS ALL SUCH
 * WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, QUIET ENJOYMENT, OR NON-INFRINGEMENT. See the RPL for specific language
 * governing rights and limitations under the RPL.
 *
 * http://opensource.org/licenses/RPL-1.5
 *
 * Copyright 2012-2015 Open Justice Broker Consortium
 */

import java.io.OutputStream;

import javax.xml.transform.OutputKeys;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

public class Main {
    /**
     * Print the specified XML DOM node to System.out
     * 
     * @param n
     *            the node to print
     * @throws Exception
     */
    public static final void printNode(Node n) throws Exception {
        printNode(n, System.out);
    }

    /**
     * Print the specified XML DOM node to the specified output stream
     * 
     * @param n
     *            the node to print
     * @param os
     *            the output stream to print to
     * @throws Exception
     */
    public static void printNode(Node n, OutputStream os) throws Exception {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        t.transform(new DOMSource(n), new StreamResult(os));
    }
}

Related

  1. print(Node node, OutputStream out)
  2. print(Node node, String encoding)
  3. print(Node node, Writer out, boolean omitdecl)
  4. print(SOAPMessage msg, OutputStream out)
  5. printAttributeValue(Element node, String prefix)
  6. printNode(Node node)
  7. printNode(Node node, String fn)
  8. printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration)
  9. printNodeSubtreeXMLString(final Node node)