Saturday 6 August 2011

Return the first named Element found. Null if none.

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

public class Utils {

  /**
   * Return a list of named Elements.
   @param element the containing Element
   @param name the tag name
   @return NodeList of matching elements
   */
  public static NodeList getElementList(Element element, String name) {
    return element.getElementsByTagName(name);
  }
  /**
   * Return the first named Element found.  Null if none.
   @param element the containing Element
   @param name the tag name
   @return matching Element (null if none)
   */
  public static Element getElement(Element element, String name) {
    NodeList nodeList = getElementList(element, name);
    return (nodeList.getLength() == 0null (ElementnodeList.item(0);
  }

}

   
    
  

No comments:

Post a Comment