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

   
    
  

No comments:

Post a Comment