Example usage for org.w3c.dom Element getTagName

List of usage examples for org.w3c.dom Element getTagName

Introduction

In this page you can find the example usage for org.w3c.dom Element getTagName.

Prototype

public String getTagName();

Source Link

Document

The name of the element.

Usage

From source file:Main.java

/**
 * Gets the list of immediate child elements with the given tag name.
 * /*w  ww  .  j  av a 2  s  .  c o  m*/
 * @param element
 * @param tagName
 * @return list of {@link Element} objects
 */
public static List<Element> getChildElementsByTagName(Element element, String tagName) {
    List<Element> elements = new ArrayList<Element>();
    NodeList list = element.getChildNodes();
    int size = list.getLength();
    if (size > 0) {
        for (int i = 0; i < size; i++) {
            Node node = list.item(i);
            if (node instanceof Element) {
                Element e = (Element) node;
                if (e.getTagName().equals(tagName)) {
                    elements.add(e);
                }
            }
        }
    }
    return elements;
}

From source file:Main.java

/**
 * Returns the number of child elements of parent that have the given
 * element tag name.// w w w . j  a  va  2  s .  c o  m
 * 
 * @param name
 *            Element tag name to search for
 * @param parent
 *            Parent to search
 * @return Count of child elements that have the given name
 */
public static int getCountChildElementsByTagName(String name, Element parent) {
    if (parent == null) {
        return 0;
    }
    NodeList children = parent.getChildNodes();
    if (children == null) {
        return 0;
    }
    int count = 0;
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            if (e.getTagName().equals(name)) {
                count++;
            }
        }
    }
    return count;
}

From source file:Main.java

/**
 * find the unique element, returns null if not found or if there is more than one
 *
 * @param tagName - tag name/*w w  w .  j  a  va  2s. c  o  m*/
 * @param root    - where to start looking
 * @return the element
 */
public static Element findElement(final String tagName, final Element root) {
    if (root.getTagName().equals(tagName)) {
        return root;
    }
    final NodeList nl = root.getElementsByTagName(tagName);
    if (nl != null && nl.getLength() == 1) {
        return (Element) nl.item(0);
    }
    return null;
}

From source file:Main.java

/**
 * Return the index'th child element of parent that has the given element
 * tag name.//from  www .j av a2s .  c  om
 * 
 * @param index
 *            Index of the child to return
 * @param name
 *            Element tag name to search for
 * @param parent
 *            Parent to search
 * @return index'th child element of parent that has the given name
 */
public static Element getChildElementByTagName(int index, String name, Element parent) {
    if (parent == null) {
        return null;
    }
    NodeList children = parent.getChildNodes();
    if (children == null) {
        return null;
    }
    int count = 0;
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
            if (e.getTagName().equals(name) && (count++ == index)) {
                return e;
            }
        }
    }
    return null;
}

From source file:Main.java

public static List<Element> getElementsByTagName(Element parent, String name, boolean localOnly) {
    List<Element> ret = new ArrayList<Element>();
    if (!localOnly) {
        NodeList elementList = parent.getElementsByTagName(name);
        for (int i = 0; i < elementList.getLength(); i++) {
            ret.add((Element) elementList.item(i));
        }//from w w w  . ja v  a 2s. c  o m
    } else {
        NodeList childList = parent.getChildNodes();
        for (int i = 0; i < childList.getLength(); i++) {
            if (childList.item(i).getNodeType() != Node.ELEMENT_NODE)
                continue;
            Element child = (Element) childList.item(i);
            if (child.getTagName().equals(name))
                ret.add(child);
        }
    }
    return ret;
}

From source file:Main.java

/**
 * Are elements equal.//  ww  w  .j av a 2 s . c o  m
 * 
 * @param element1
 *            the element1
 * @param element2
 *            the element2
 * @return true, if successful
 */
public static boolean areElementsEqual(Element element1, Element element2) {
    if (!element1.getTagName().equals(element2.getTagName())) {
        return false;
    }
    NamedNodeMap nodeAttrMap = element1.getAttributes();
    NamedNodeMap pathAttrMap = element2.getAttributes();
    if ((nodeAttrMap == null && pathAttrMap == null)
            || (pathAttrMap.getLength() == 0 && nodeAttrMap.getLength() == 0)) {
        return true;
    } else {
        if (element1.hasAttribute("name") && element2.hasAttribute("name")) {
            if (element1.getAttribute("name").equals(element2.getAttribute("name"))) {
                return true;
            }
        } else if (nodeAttrMap != null && pathAttrMap != null
                && (nodeAttrMap.getLength() == pathAttrMap.getLength())) {
            for (int k = 0; k < nodeAttrMap.getLength(); k++) {
                Node nodeAttr = nodeAttrMap.item(k);
                String nodeAttrName = nodeAttr.getNodeName();
                String nodeAttrValue = nodeAttr.getNodeValue();
                if (element2.hasAttribute(nodeAttrName)
                        && nodeAttrValue.equals(element2.getAttribute(nodeAttrName))) {
                    return true;
                }
            }
        }
    }
    return false;
}

From source file:Main.java

public static boolean is(Element element, QName qName) {
    final boolean equals = qName.equals(new QName(element.getNamespaceURI(), element.getTagName()));
    return equals;
}

From source file:Main.java

/**
 * Retrieves the given DOM element's first child element with the specified
 * name. If name is null, the first child of any type is returned.
 */// w w w. j av  a  2  s .  c o m
public static Element getFirstChild(final Element el, final String name) {
    final NodeList nodes = el.getChildNodes();
    final int len = nodes.getLength();
    for (int i = 0; i < len; i++) {
        final Node node = nodes.item(i);
        if (!(node instanceof Element))
            continue;
        final Element e = (Element) node;
        if (name == null || e.getTagName().equals(name))
            return e;
    }
    return null;
}

From source file:Main.java

/**
 * Return an array of direct child elements that have the given element tag
 * name./*from   w ww  .  j  a  v a 2 s. c om*/
 * 
 * @param name
 *            Element tag name to search for
 * @param parent
 *            Parent to search
 * @return Array of child elements with the given name
 */
public static Element[] getChildElementsByTagName(String name, Element parent) {
    List<Element> elements = new ArrayList<Element>();
    if (parent != null) {
        NodeList children = parent.getChildNodes();
        if (children != null) {
            for (int i = 0; i < children.getLength(); i++) {
                Node node = children.item(i);
                if (node instanceof Element) {
                    Element e = (Element) node;
                    if (e.getTagName().equals(name)) {
                        elements.add(e);
                    }
                }
            }
        }
    }
    return elements.toArray(new Element[elements.size()]);
}

From source file:Main.java

/**
 * Find an immediate child of the given name
 *//*  w  w  w .j  a v  a 2s. com*/
public static Element findImmediateChildElement(Node node, String name) {

    Element found = null;

    if (node != null) {

        for (Node child = node.getFirstChild(); child != null
                && found == null; child = child.getNextSibling()) {

            if (child.getNodeType() == Node.ELEMENT_NODE) {
                Element tmp = (Element) child;
                if (tmp.getTagName().equals(name)) {
                    return tmp;
                }
            }
        }
    }

    return found;
}