Saturday 6 August 2011

Returns the text of the element

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

public class Utils {

  /**
   
   */
  public static String getElementText(Element element) {
      StringBuffer buffer = new StringBuffer();
      NodeList nodeList = element.getChildNodes();
      for (int i = 0; i < nodeList.getLength(); i++) {
          Node node = nodeList.item(i);
          if (node.getNodeType() == Node.TEXT_NODE || node.getNodeType() == Node.CDATA_SECTION_NODE) {
              buffer.append(node.getNodeValue());
          }
      }
      return buffer.toString();
  }
}

   
    
  

Returns the first element that has the specified local name.

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

public class Utils {

  public static Element getFirstChild(Element e, String local) {
    forNode n=e.getFirstChild(); n!=null; n=n.getNextSibling() ) {
        if(n.getNodeType()==Node.ELEMENT_NODE) {
            Element c = (Element)n;
            if(c.getLocalName().equals(local))
                return c;
        }
    }
    return null;
}

}

   
    
  

Returns the first child element with the given name.

  

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

public class Utils {


  /**
   * <p>Returns the first child element with the given name. Returns
   * <code>null</code> if not found.</p>
   *
   @param parent parent element
   @param name name of the child element
   @return child element
   */
  public static Element getChildElementByName(Element parent, String name)
  {
      NodeList children = parent.getChildNodes();

      for(int i = 0; i < children.getLength(); i++) {
          Node node = children.item(i);
          if(node.getNodeType() == Node.ELEMENT_NODE) {
              Element element = (Elementnode;
              if(element.getTagName().equals(name)) {
                  return element;
              }
          }
      }

      return null;
  }

}

   
    
  

Returns text value of a child element. Returns null if there is no child element found.

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

public class Utils {
  public static String getElementText(Element element)
  {
      StringBuffer buf = new StringBuffer();

      NodeList children = element.getChildNodes();
      for(int i = 0; i < children.getLength(); i++) {
          Node node = children.item(i);
          if(node.getNodeType() == Node.TEXT_NODE ||
             node.getNodeType() == Node.CDATA_SECTION_NODE) {
              Text text = (Textnode;
              buf.append(text.getData().trim());
          }
      }

      return buf.toString();
  }


}

   
    
  

Returns an array of text values of a child element.

  
import java.util.ArrayList;
import java.util.List;

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

public class Utils {

  /**
   * <p>Returns an array of text values of a child element. Returns
   * <code>null</code> if there is no child element found.</p>
   *
   @param parent parent element
   @param name name of the child element
   @return text value
   */
  public static String[] getChildElementTextArr(Element parent, String name)
  {
      // Get all the elements
      List children = getChildElementsByName(parent, name);

      String str[] new String[children.size()];

      for(int i = 0; i < children.size(); i++) {
          Node child = (Nodechildren.get(i);

          StringBuffer buf = new StringBuffer();

          NodeList nodes = child.getChildNodes();
          for(int j = 0; j < nodes.getLength(); j++) {
              Node node = nodes.item(j);
              if(node.getNodeType() == Node.TEXT_NODE ||
                 node.getNodeType() == Node.CDATA_SECTION_NODE) {
                  Text text = (Textnode;
                  buf.append(text.getData().trim());
              }
          }

          str[i= buf.toString();
      }

      return str;
  }
  /**
   * <p>Returns a list of child elements with the given
   * name. Returns an empty list if there are no such child
   * elements.</p>
   *
   @param parent parent element
   @param name name of the child element
   @return child elements
   */
  public static List getChildElementsByName(Element parent, String name)
  {
      List elements = new ArrayList();

      NodeList children = parent.getChildNodes();

      for(int i = 0; i < children.getLength(); i++) {
          Node node = children.item(i);
          if(node.getNodeType() == Node.ELEMENT_NODE) {
              Element element = (Elementnode;
              if(element.getTagName().equals(name)) {
                  elements.add(element);
              }
          }
      }

      return elements;
  }

}

   
    
  

Returns a list of child elements with the given name.

  
import java.util.ArrayList;
import java.util.List;

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

public class Utils {


  /**
   * <p>Returns a list of child elements with the given
   * name. Returns an empty list if there are no such child
   * elements.</p>
   *
   @param parent parent element
   @param name name of the child element
   @return child elements
   */
  public static List getChildElementsByName(Element parent, String name)
  {
      List elements = new ArrayList();

      NodeList children = parent.getChildNodes();

      for(int i = 0; i < children.getLength(); i++) {
          Node node = children.item(i);
          if(node.getNodeType() == Node.ELEMENT_NODE) {
              Element element = (Elementnode;
              if(element.getTagName().equals(name)) {
                  elements.add(element);
              }
          }
      }

      return elements;
  }

}

   
    
  

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);
  }

}