Here you can find the source of findComponent(Element element, String attributeValue)
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 |
public static Element findComponent(Element element, String attributeValue)
//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; } }