Java XML Attribute Print printAttributes(NamedNodeMap attributes, String indent, boolean descend)

Here you can find the source of printAttributes(NamedNodeMap attributes, String indent, boolean descend)

Description

print Attributes

License

Open Source License

Declaration

@SuppressWarnings("unused")
    private static void printAttributes(NamedNodeMap attributes, 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 {
    @SuppressWarnings("unused")
    private static void printAttributes(NamedNodeMap attributes, String indent, boolean descend) {

        System.out.println();/*from  w  ww. j ava 2  s.c  o m*/
        for (int i = 0; i < attributes.getLength(); ++i) {
            System.out.println(indent + "-- Attribute: " + i + " -----");
            printToTerminal(attributes.item(i), indent + "  ", descend);
        }

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

    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");
        }
    }

    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. printAttribute(Node node, int level)
  2. printAttribute(String nombre, NodeList nodes)
  3. printAttribute(String nombre, NodeList nodes)
  4. printAttribute(XMLStreamReader xmlr, int index, StringBuffer result)
  5. printAttributes(Element element)
  6. printAttributes(XMLStreamReader xmlr)
  7. printAttributesCompact(NamedNodeMap attributes, String indent, boolean descend)
  8. printAttrs(Element elem, String endient, PrintStream pstrm)
  9. printImageAttributeType(Integer value)