Java XML Element Get by Attribute findElement(Element element, String elementName, String attributeValue)

Here you can find the source of findElement(Element element, String elementName, String attributeValue)

Description

If there is no such element, one will be created on element .

License

Apache License

Parameter

Parameter Description
element the root element from which to start searching
elementName element name of an Element object which to find
attributeValue attribute value of the name attribute on the elementName tag which to find

Return

an object named elementName which has an attribute named attributeValue

Declaration

public static Element findElement(Element element, String elementName, String attributeValue) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.*;

public class Main {
    /**/*from w  ww .  ja va  2s  .c  o m*/
     * If there is no such element, one will be created on {@code element}.
     * @param element the root element from which to start searching
     * @param elementName element name of an {@link Element} object which to find
     * @param attributeValue attribute value of the {@literal name} attribute on the {@code elementName} tag which to find
     * @return an {@link Element} object named {@code elementName} which has an attribute named {@code attributeValue}
     */
    public static Element findElement(Element element, String elementName, String attributeValue) {
        NodeList children = element.getElementsByTagName(elementName);
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (!(child instanceof Element)) {
                continue;
            }
            Element childElement = (Element) child;
            if (childElement.hasAttribute("name") && attributeValue.equals(childElement.getAttribute("name"))) {
                return childElement;
            }
        }
        Element createdElement = createElement(element, elementName);
        createdElement.setAttribute("name", attributeValue);
        return createdElement;
    }

    /**
     * If there is no such element, one will be created on {@code element}.
     * @param element the root element from which to start searching
     * @param elementName element name of an {@link Element} object which to find
     * @return an {@link Element} object named {@code elementName} on {@code element}
     */
    public static Element findElement(Element element, String elementName) {
        NodeList elements = element.getElementsByTagName(elementName);
        if ((elements == null) || (elements.getLength() == 0)) {
            return createElement(element, elementName);
        }
        // return first if more than one...TODO - is that ok?
        return (Element) elements.item(0);
    }

    protected static Element createElement(Element element, String name) {
        Element child = element.getOwnerDocument().createElement(name);
        element.appendChild(child);
        return child;
    }
}

Related

  1. findAllElementsByAttribute(Node node, String tagName, String attrName, String attrValue)
  2. findAllElementsByAttributes(Element node, String tagName, String attrName, List attrValues)
  3. findComponent(Element element, String attributeValue)
  4. findElement(Element parent, String elementNS, String elementName, String attrName, String attrValue)
  5. findElementByAttribute(final NodeList list, final String name, final String value)
  6. findElementsByAttribute(Element node, String tagName, String attrName, List attrValues)
  7. findElementWithAttributes(Node node, String tagName, Collection attrs)