Get the name from the supplied XML element. - Java XML

Java examples for XML:DOM Element

Description

Get the name from the supplied XML element.

Demo Code

// This library is free software; you can redistribute it and/or
//package com.java2s;
import org.w3c.dom.Element;

public class Main {
    /**//from ww  w  . j  av a 2 s.  c o m
     * Get the name from the supplied element.
     * <p/>
     * Returns the {@link Node#getLocalName() localName} of the element
     * if set (namespaced element), otherwise the
     * element's {@link Element#getTagName() tagName} is returned.
     * <p/>
     * <b>NOTE</b>: Taken from Milyn Smooks.
     *
     * @param element The element.
     * @return The element name.
     */
    public static String getName(Element element) {

        String name = element.getLocalName();

        if (name != null) {
            return name;
        } else {
            return element.getTagName();
        }
    }
}

Related Tutorials