Java XML Element Find findElement(Element element, String elementName)

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

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

Return

an object named elementName on element

Declaration

public static Element findElement(Element element, String elementName) 

Method Source Code


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

import org.w3c.dom.*;

public class Main {
    /**//from  w w  w.j a v  a 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. findAllElementsByTagName(Element elem, String tagName)
  2. findAllElementsByTagNameNS(Element el, String nameSpaceURI, String localName, List elementList)
  3. findComponentElement(Element root)
  4. findComponentElement(Element root)
  5. findElement(Element element, String name)
  6. findElement(Element root, String localName, String namespace)
  7. findElement(Element topElm, String localName, String namespace)
  8. findElement(final Element e, final String find)