Prints the XML tree that is rooted at the specified node to the system default OutputStream (stdout). - Java XML

Java examples for XML:DOM Node

Description

Prints the XML tree that is rooted at the specified node to the system default OutputStream (stdout).

Demo Code


//package com.java2s;
import java.io.PrintStream;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

public class Main {
    /**// ww w.j  av a2  s .  com
     * Prints the XML tree that is rooted at the specified node to the system default
     * OutputStream (stdout).
     *
     * @param  n  The node that contains the XML tree that is to be printed out.
     * @pre    n != null
     */
    public static void printXMLTree(Node n) {
        printXMLTree(n, System.out);
    }

    /**
     * Prints the XML tree that is rooted at the specified node to the specified
     * PrintStream.
     *
     * @param  n    The node that contains the XML tree that is to be printed out.
     * @param  out  The PrintStream to which a dump of the XML tree is to be written.
     * @pre    n != null && out != null
     */
    public static void printXMLTree(Node n, PrintStream out) {
        printXMLNode(n, out);
        printXMLTreeRec(n, out, "");

    }

    /**
     * Prints a textual representation of the given node to the specified PrintStream.
     *
     * @param  n    Node that is to be printed.
     * @param  out  The PrintStream to which the node is to be printed.
     * @pre    n != null && out != null
     */
    public static void printXMLNode(Node n, PrintStream out) {
        switch (n.getNodeType()) {
        case Node.DOCUMENT_NODE:
            out.println("DOC_ROOT");
            break;

        case Node.ELEMENT_NODE:
            out.println("<" + ((Element) n).getTagName() + ">");
            break;

        case Node.ATTRIBUTE_NODE:
            out.println("@" + ((Attr) n).getName());
            break;

        case Node.TEXT_NODE:
            out.println("\"" + ((Text) n).getWholeText().trim() + "\"");
            break;

        case Node.COMMENT_NODE:
            out.println("COMMENT: \"" + n.getTextContent().trim() + "\"");
            break;

        default:
            out.println("Unknown node type: " + n.getNodeType());
        }
    }

    private static void printXMLTreeRec(Node n, PrintStream out, String pre) {
        final String NO_EDGE = "  ";
        final String STRAIGHT_EDGE = "? ";
        final String CHILD_EDGE = "??";
        final String LAST_CHILD_EDGE = "??";

        // Get node contents.
        NodeList children = n.getChildNodes();
        NamedNodeMap attributes = n.getAttributes();

        // Print attributes (if any).
        Node attr;
        if (attributes != null) {
            for (int index = 0; index < attributes.getLength(); index++) {
                attr = attributes.item(index);

                // Last child?
                if (index + 1 == children.getLength()
                        && children.getLength() == 0) {
                    out.print(pre + LAST_CHILD_EDGE);
                    printXMLNode(attr, out);
                    printXMLTreeRec(attr, out, pre + NO_EDGE);
                } else {
                    out.print(pre + CHILD_EDGE);
                    printXMLNode(attr, out);
                    printXMLTreeRec(attr, out, pre + STRAIGHT_EDGE);
                }
            }
        }

        // Print children.
        Node child;
        for (int index = 0; index < children.getLength(); index++) {
            child = children.item(index);

            // Last child?
            if (index + 1 == children.getLength()) {
                out.print(pre + LAST_CHILD_EDGE);
                printXMLNode(child, out);
                printXMLTreeRec(child, out, pre + NO_EDGE);
            } else {
                out.print(pre + CHILD_EDGE);
                printXMLNode(child, out);
                printXMLTreeRec(child, out, pre + STRAIGHT_EDGE);
            }
        }
    }
}

Related Tutorials