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

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

Description

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

License

Apache License

Parameter

Parameter Description
element the root element from which to start searching
attributeValue attribute value of the name attribute on the <component> Element object which to find

Return

an a object with attribute named attributeName

Declaration

public static Element findComponent(Element element, String attributeValue) 

Method Source Code


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

import org.w3c.dom.*;

public class Main {
    /**/*from  w  w w . j  av  a 2 s.  c  o m*/
     * If there is no such {@link Element} object one will be created on {@code element}.
     * @param element the root element from which to start searching
     * @param attributeValue attribute value of the {@literal name} attribute on the {@literal <component>}
     *                       {@link Element} object which to find
     * @return an a {@literal <component>} {@link Element} object with attribute named {@code attributeName}
     */
    public static Element findComponent(Element element, String attributeValue) {
        return findElement(element, "component", attributeValue);
    }

    /**
     * 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. findElement(Element element, String elementName, 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)