Java XML Element Print printElement(Element e)

Here you can find the source of printElement(Element e)

Description

Utility method for displaying an Element

License

Open Source License

Declaration

public static void printElement(Element e) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**/*  ww  w.  ja v  a2  s .  c o m*/
     * Utility method for displaying an Element
     */
    public static void printElement(Element e) {
        printElement(e, "");
    }

    /**
     * Utility method for displaying an Element, with indent control
     */
    public static void printElement(Element e, String prefix) {
        System.out.print(prefix + e.getTagName() + " ");
        NamedNodeMap attrs = e.getAttributes();
        for (int i = 0; i < attrs.getLength(); i++) {
            Node att = attrs.item(i);
            System.out.print(att.getNodeName() + "=" + att.getNodeValue() + " ");
        }
        System.out.println();

        NodeList children = e.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child instanceof Element)
                printElement((Element) child, prefix + "  ");
            else
                System.out.println(prefix + child.getNodeName() + " " + child.getNodeValue());
        }
    }
}

Related

  1. printElement(SOAPElement el)
  2. printElementBody(Element elem, StringWriter sw)
  3. printElementEnd(Element elem, StringWriter sw)
  4. printElementStart(Element elem, StringWriter sw)