Recursive method to find a given attribute value : DOM Attribute « XML « Java






Recursive method to find a given attribute value

  
import javax.xml.namespace.QName;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Utils {

  /**
   * Recursive method to find a given attribute value
   */
  public static String recursiveGetAttributeValue(Element element, String attributeName) {
      String answer = null;
      try {
          answer = element.getAttribute(attributeName);
      } catch (Exception e) {
    
      }
      if (answer == null || answer.length() == 0) {
          Node parentNode = element.getParentNode();
          if (parentNode instanceof Element) {
              return recursiveGetAttributeValue((Element) parentNode, attributeName);
          }
      }
      return answer;
  }
}

   
    
  








Related examples in the same category

1.Get all the attributes for an Element
2.Get attribute's value
3.Return the right attribute node
4.Return the value of the attribute of the given element with the given name
5.Output XML element Attributes
6.Set Attribute
7.Set an Attribute in an Element
8.Copy the attribues on one element to the other
9.Get Attribute
10.Get Attribute by QName
11.Get an Attribute from an Element. Returns an empty String if none found
12.remove Attribute
13.Find Container With Attribute Value Or Create
14.Find Container With Attribute Value Or Create And Set
15.Find the first direct child with a given attribute.
16.Returns null, not "", for a nonexistent attribute
17.Retutns the value of the named attribute of the given element.
18.An Attributes implementation that can perform more operations than the attribute list helper supplied with the standard SAX2 distribution.