Java Utililty Methods XML Element Get by Attribute

List of utility methods to do XML Element Get by Attribute

Description

The list of methods to do XML Element Get by Attribute are organized into topic(s).

Method

ListfindAllElementsByAttribute(Node node, String tagName, String attrName, String attrValue)
Reqursion search in node for elements with paramantrs
List<Element> result = new LinkedList<Element>();
findAllElementsByAttribute(node, tagName, attrName, attrValue, result);
return result;
ListfindAllElementsByAttributes(Element node, String tagName, String attrName, List attrValues)
find All Elements By Attributes
List<Element> result = new ArrayList<Element>();
findAllElementsByAttributes(node, tagName, attrName, attrValues, result);
return result;
ElementfindComponent(Element element, String attributeValue)
If there is no such Element object one will be created on element .
return findElement(element, "component", attributeValue);
ElementfindElement(Element element, String elementName, String attributeValue)
If there is no such element, one will be created on element .
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"))) {
...
ElementfindElement(Element parent, String elementNS, String elementName, String attrName, String attrValue)
find Element
NodeList l = parent.getElementsByTagNameNS(elementNS, elementName);
for (int i = 0; i < l.getLength(); i++) {
    Element e = (Element) l.item(i);
    String val = e.getAttribute(attrName);
    if (val != null && val.equals(attrValue)) {
        return e;
return null;
ElementfindElementByAttribute(final NodeList list, final String name, final String value)
find Element By Attribute
for (int i = 0; i < list.getLength(); ++i) {
    if (list.item(i).getNodeType() == Node.ELEMENT_NODE) {
        final Element e = (Element) list.item(i);
        if (e.hasAttribute(name) && e.getAttribute(name).equals(value)) {
            return e;
return null;
ListfindElementsByAttribute(Element node, String tagName, String attrName, List attrValues)
find Elements By Attribute
List<Element> result = new LinkedList<Element>();
NodeList nodeList = node.getChildNodes();
if (nodeList == null) {
    return result;
for (int i = 0; i < nodeList.getLength(); ++i) {
    Element element = checkIfElement(nodeList.item(i), tagName);
    if (element != null) {
...
ElementfindElementWithAttributes(Node node, String tagName, Collection attrs)
find Element With Attributes
Element result = null;
NodeList nodeList = node.getChildNodes();
if (nodeList == null) {
    return result;
for (int i = 0; i < nodeList.getLength(); ++i) {
    Element element = checkIfElement(nodeList.item(i), tagName);
    if (element != null) {
...
ElementfindElementWithNameAttribute(Element parent, String name)
Trys to find a child element in the given parent element where the child's name attribute is the given value.
NodeList l = parent.getChildNodes();
for (int i = 0; i < l.getLength(); i++) {
    Node n = l.item(i);
    if (n.getNodeType() == n.ELEMENT_NODE) {
        Element e = (Element) n;
        if (e.getAttribute("name").equals(name)) {
            return e;
return null;
ElementfindElementWithUniqueAttribute(Element root, String elementName, String attribute, String attributeValue)
find Element With Unique Attribute
NodeList list = root.getElementsByTagName(elementName);
for (int i = 0; i < list.getLength(); i++) {
    Element tmp = (Element) list.item(i);
    if (tmp.getAttribute(attribute).equals(attributeValue)) {
        return tmp;
return null;
...