Java XML Node Print printToTerminal(Node n, String indent, boolean descend)

Here you can find the source of printToTerminal(Node n, String indent, boolean descend)

Description

print To Terminal

License

Open Source License

Declaration

public static void printToTerminal(Node n, String indent, boolean descend) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static void printToTerminal(Node n, String indent, boolean descend) {

        if (n != null) {

            System.out.println(indent + "Name: " + n.getNodeName());
            System.out.println(indent + "NS  : " + n.getNamespaceURI());
            System.out.println(indent + "txt : " + n.getTextContent());
            System.out.println(indent + "Type: " + n.getNodeType());
            System.out.println(indent + "Val : [" + n.getNodeValue() + "]");

            if (n.hasAttributes())
                printAttributesCompact(n.getAttributes(), indent, descend);

            if (descend && n.hasChildNodes())
                printChildNode(n.getChildNodes(), indent, descend);

            if (indent == "")
                System.out.println(indent + "________________________\n");
        }//w w  w  . j  a v a2s  . c  om
    }

    private static void printAttributesCompact(NamedNodeMap attributes, String indent, boolean descend) {

        System.out.println(indent + "Attributes: " + getAttributesCompact(attributes));
    }

    private static void printChildNode(NodeList childNodes, String indent, boolean descend) {

        System.out.println();
        for (int i = 0; i < childNodes.getLength(); ++i) {
            System.out.println(indent + "-- Child: " + i + " ---------");
            printToTerminal(childNodes.item(i), indent + "  ", descend);
        }

        System.out.println(indent + "-- ^last child ------\n");
    }

    private static String getAttributesCompact(NamedNodeMap attributes) {

        String s = "[";
        for (int i = 0; i < attributes.getLength(); ++i) {
            Node n = attributes.item(i);
            s += n.getNodeName() + "=\"" + n.getNodeValue() + "\"";
            s += ", ";
        }
        s += "]";

        return s;
    }
}

Related

  1. printNode(Node node, StringWriter sw)
  2. printNodeBasics(Node node)
  3. printNodeIterator(NodeIterator iterator)
  4. printNodes(Node node, int level)
  5. printNodeType(Node node, int ident)
  6. printTree(Node node, int ident)
  7. printTree(Node root, int level)