Java XML Node Print printNode(Node node, PrintStream out)

Here you can find the source of printNode(Node node, PrintStream out)

Description

Print out the node in XML format

License

Open Source License

Parameter

Parameter Description
node a parameter
out a parameter

Declaration

public static void printNode(Node node, PrintStream out) 

Method Source Code


//package com.java2s;
/*//from  w  w  w . ja  va2 s . c om
 * Misc-Utils - Miscellaneous Utility Classes
 * Copyright (C) 2007 Newisys, Inc. or its licensors, as applicable.
 * Java is a registered trademark of Sun Microsystems, Inc. in the U.S. or
 * other countries.
 *
 * Licensed under the Open Software License version 3.0 (the "License"); you
 * may not use this file except in compliance with the License. You should
 * have received a copy of the License along with this software; if not, you
 * may obtain a copy of the License at
 *
 * http://opensource.org/licenses/osl-3.0.php
 *
 * This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

import java.io.IOException;
import java.io.PrintStream;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.w3c.dom.Node;

import com.sun.org.apache.xerces.internal.dom.DocumentImpl;
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class Main {
    /**
     * Print out the node in XML format
     * 
     * @param node
     * @param out
     */
    public static void printNode(Node node, PrintStream out) {
        Document doc = new DocumentImpl();
        Element topLevel = doc.createElement("xml");
        doc.appendChild(topLevel);
        topLevel.appendChild(doc.importNode(node, true));
        OutputFormat format = new OutputFormat(doc);
        format.setLineWidth(65);
        format.setIndenting(true);
        format.setIndent(2);
        XMLSerializer serializer = new XMLSerializer(out, format);
        try {
            serializer.serialize(doc);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }

    }
}

Related

  1. printNode(Node node)
  2. printNode(Node node)
  3. printNode(Node node)
  4. printNode(Node node)
  5. printNode(Node node, int level)
  6. printNode(Node node, StringWriter sw)
  7. printNodeBasics(Node node)
  8. printNodeIterator(NodeIterator iterator)
  9. printNodes(Node node, int level)