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

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

Description

For debug purpose.

License

Open Source License

Parameter

Parameter Description
node starting Node
out where to write the result, for example System.out

Declaration

public static void printDOMTree(Node node, PrintStream out)

    

Method Source Code


//package com.java2s;

import java.io.PrintStream;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**/*from   ww w . jav  a2s. c o m*/
     * For debug purpose. Returns a graphical representation of a given DOM Tree
     * starting at node.
     * 
     * @param node
     *            starting Node
     * @param out
     *            where to write the result, for example System.out
     */
    public static void printDOMTree(Node node, PrintStream out)

    {
        int type = node.getNodeType();
        switch (type) {
        // print the document element
        case Node.DOCUMENT_NODE: {
            printDOMTree(((Document) node).getDocumentElement(), out);
            break;
        }

        // print element with attributes
        case Node.ELEMENT_NODE: {
            out.print("<");
            out.print(node.getNodeName());
            NamedNodeMap attrs = node.getAttributes();
            for (int i = 0; i < attrs.getLength(); i++) {
                Node attr = attrs.item(i);
                out.print(" " + attr.getNodeName() + "=\"" + attr.getNodeValue() + "\"");
            }
            out.print(">");

            NodeList children = node.getChildNodes();
            if (children != null) {
                int len = children.getLength();
                for (int i = 0; i < len; i++)
                    printDOMTree(children.item(i), out);
            }

            break;
        }

        // handle entity reference nodes
        case Node.ENTITY_REFERENCE_NODE: {
            out.print("&");
            out.print(node.getNodeName());
            out.print(";");
            break;
        }

        // print cdata sections
        case Node.CDATA_SECTION_NODE: {
            out.print("<![CDATA[");
            out.print(node.getNodeValue());
            out.print("]]>");
            break;
        }

        // print text
        case Node.TEXT_NODE: {
            out.print(node.getNodeValue());
            break;
        }

        // print processing instruction
        case Node.PROCESSING_INSTRUCTION_NODE: {
            out.print("<?");
            out.print(node.getNodeName());
            String data = node.getNodeValue();
            {
                out.print(" ");
                out.print(data);
            }
            out.print("?>");
            break;
        }
        }

        if (type == Node.ELEMENT_NODE) {
            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }
    }
}

Related

  1. print(Node node)
  2. printArrayContent(Node[] t)
  3. printDifferences(Node node1, Node node2)
  4. printDOM(Node node)
  5. printDOM(Node node, String prefix)
  6. printElement(Node node, int level)
  7. printlnCommon(Node n)
  8. printNode(final String indent, final Node node)
  9. printNode(Node aNode, int aLevel)