Java XML Node Text Value getTextContent(Node element)

Here you can find the source of getTextContent(Node element)

Description

Extracts the text from the given element.

License

Apache License

Declaration

public static String getTextContent(Node element) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    /**/*from www  .  jav a  2s . c  om*/
     * Extracts the text from the given element.
     * Element.getTextContet() is java5 specific, so we need to use this until we drop 1.4 support.
     */
    public static String getTextContent(Node element) {
        StringBuffer text = new StringBuffer();
        NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node child = childNodes.item(i);
            if (child instanceof Text) {
                text.append(child.getNodeValue());
            }
        }

        return text.toString();
    }
}

Related

  1. getTextContent(final Node node, final String defaultValue)
  2. getTextContent(final Node node, final StringBuffer sb)
  3. getTextContent(final Node xmlNode)
  4. getTextContent(Node baseNode)
  5. getTextContent(Node e)
  6. getTextContent(Node node)
  7. getTextContent(Node node)
  8. getTextContent(Node node)
  9. getTextContent(Node node)