Prints the XML tree that is rooted at the specified node to the specified PrintStream. - Java XML

Java examples for XML:DOM Node

Description

Prints the XML tree that is rooted at the specified node to the specified PrintStream.

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 {

    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;//  w w  w  .  j a  v  a 2  s  .  com
        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);
            }
        }
    }

    /**
     * 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());
        }
    }
}

Related Tutorials