Java XML Node Text Value getContentsOfTextOnlyNode(Node n)

Here you can find the source of getContentsOfTextOnlyNode(Node n)

Description

get Contents Of Text Only Node

License

Open Source License

Return

one large string with the contents of all TextNodes, or null if there are non text nodes or no text nodes as children.

Declaration

public static String getContentsOfTextOnlyNode(Node n) 

Method Source Code

//package com.java2s;
/**//  w  ww.  j av  a  2s.  com
 * This file belongs to the BPELUnit utility and Eclipse plugin set. See enclosed
 * license file for more information.
 */

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

public class Main {
    /**
     * 
     * @return one large string with the contents of all TextNodes, or null if
     *         there are non text nodes or no text nodes as children.
     */
    public static String getContentsOfTextOnlyNode(Node n) {
        NodeList children = n.getChildNodes();
        if (children.getLength() == 0) {
            return null;
        }

        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < children.getLength(); i++) {
            Node node = children.item(i);
            if (node.getNodeType() == Node.TEXT_NODE) {
                sb.append(node.getNodeValue());
            } else {
                return null;
            }
        }

        return sb.toString();
    }
}

Related

  1. extractNodeText(Node node)
  2. extractText(Node node)
  3. findNodeText(Node node)
  4. get_inner_text(Node node)
  5. getAppInfoText(final Node node)
  6. getContentText(Node n)
  7. getDescendentText(Node node, String name)
  8. getDirectText(org.w3c.dom.Element node)
  9. getElementText(Node elem)