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

NodefindNode(Node node, String attr, String value)
Returns the first node where attr equals value.
String tmp = (node instanceof Element) ? ((Element) node).getAttribute(attr) : null;
if (tmp != null && tmp.equals(value)) {
    return node;
node = node.getFirstChild();
while (node != null) {
    Node result = findNode(node, attr, value);
    if (result != null) {
...
ElementfindNodeByAttributeValue(NodeList nodeList, String attributeName, String attributeValue)
find Node By Attribute Value
int len = nodeList.getLength();
String tmp;
Element property;
for (int i = 0; i < len; i++) {
    property = (Element) nodeList.item(i);
    tmp = property.getAttribute(attributeName);
    if (attributeValue.equals(tmp)) {
        return property;
...
ElementgetElementByAttribute(Element root, String tagname, String attribute, String att_value)
get Element By Attribute
NodeList nl = root.getElementsByTagName(tagname);
for (int i = 0; i < nl.getLength(); i++) {
    Element elem = (Element) nl.item(i);
    if (elem.getAttribute(attribute).equals(att_value)) {
        return elem;
return null;
...
ElementgetElementByAttribute(String attr, String value, Element root)
get Element By Attribute
Element found = null;
if (root.hasAttribute(attr) && root.getAttribute(attr).equals(value)) {
    return root;
NodeList nl = root.getChildNodes();
for (int i = nl.getLength() - 1; i >= 0; i--) {
    if (nl.item(i).getNodeType() == Node.ELEMENT_NODE) {
        found = getElementByAttribute(attr, value, (Element) nl.item(i));
...
ElementgetElementByAttributeValue(List elements, String attName, String attValue)
get Element By Attribute Value
for (Element e : elements) {
    String s = e.getAttribute(attName);
    if (attValue.equals(s))
        return e;
return null;
ElementgetElementByAttributeValue(Node start, String tagName, String attrName, String attrValue)
equivalent to the XPath expression './/tagName[@attrName='attrValue']'
NodeList nl = ((Element) start).getElementsByTagName(tagName);
int l = nl.getLength();
if (l == 0) {
    return null;
Element e = null;
String compareValue = null;
for (int i = 0; i < l; i++) {
...
intgetElementIntAttribute(Element e, String whichAttribute)
get Element Int Attribute
int val = -1;
String number = e.getAttribute(whichAttribute);
if (number != null) {
    try {
        val = Integer.parseInt(number.trim());
    } catch (NumberFormatException ex) {
        val = -1;
return val;
LinkedListgetElementsWithAttribute(Element element, String attribute)
Get the elements descendant having the specified attribute name.
LinkedList elements = new LinkedList();
if (element.hasAttribute(attribute)) {
    elements.add(element);
NodeList childs = element.getChildNodes();
for (int i = 0; i < childs.getLength(); i++) {
    Node node = childs.item(i);
    if (Node.ELEMENT_NODE == node.getNodeType()) {
...
voidgetElementsWithAttribute(Element element, String namespaceURI, String localName, String value, Collection elements)
get Elements With Attribute
if (element.hasAttributeNS(namespaceURI, localName)) {
    String attr = element.getAttributeNS(namespaceURI, localName);
    if (attr.equals(value))
        elements.add(element);
NodeList childs = element.getChildNodes();
for (int i = 0; i < childs.getLength(); i++) {
    Node node = childs.item(i);
...
ListgetElementsWithAttributeEquals(Element root, String attribute, String value)
Retrieve the list of the elements which have an attribute equal to the given value.
List<Element> list = new ArrayList<Element>();
getElementsWithAttributeEquals(root, attribute, value, list);
return list;