Java XML Node Text Value getTextData(Node node)

Here you can find the source of getTextData(Node node)

Description

get Text Data

License

Apache License

Declaration

public static String getTextData(Node node) 

Method Source Code

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

import org.w3c.dom.CDATASection;

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

public class Main {

    public static String getTextData(Node node) {
        if (!node.hasChildNodes()) {
            return null;
        }/*  w  ww .  j  av  a 2 s  .  c o  m*/
        Node child = node.getFirstChild();
        while (child != null && child.getNodeType() != Node.TEXT_NODE
                && child.getNodeType() != Node.CDATA_SECTION_NODE) {
            child = child.getNextSibling();
        }
        if (child == null) {
            return null;
        }

        if (child.getNodeType() == Node.TEXT_NODE) {
            return ((Text) child).getData();
        } else {
            return ((CDATASection) child).getData();
        }

    }
}

Related

  1. getTextContent(Node node, String path)
  2. getTextContent(org.w3c.dom.Node element)
  3. getTextContentOld(final Node node)
  4. getTextContents(Node node)
  5. getTextData(Node element)
  6. getTextForNode(Node node)
  7. getTextForNode(Node node)
  8. getTextForNode(Node node)
  9. getTextFromNode(Node node, String defaultValue)