Java XML Node Text Value getText(Node node)

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

Description

Gets text for a node.

License

Open Source License

Parameter

Parameter Description
node Node.

Return

Value of first child text node, or the empty string if none found, with leading and trailing white space trimmed.

Declaration


public static String getText(Node node) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

import org.w3c.dom.*;

public class Main {
    /**   Gets text for a node.
     *//from   ww w.  j  ava2 s  . com
     *   @param   node      Node.
     *
     *   @return            Value of first child text node, or the empty
     *                  string if none found, with leading and trailing
     *                  white space trimmed.
     */

    public static String getText(Node node) {
        NodeList children = node.getChildNodes();
        int numChildren = children.getLength();
        for (int i = 0; i < numChildren; i++) {
            Node child = children.item(i);
            if (child.getNodeType() == Node.TEXT_NODE)
                return child.getNodeValue().trim();
        }
        return "";
    }
}

Related

  1. getText(Node n)
  2. getText(Node n)
  3. getText(Node n)
  4. getText(Node nd)
  5. getText(Node nd, StringBuilder buf)
  6. getText(Node node)
  7. getText(Node node)
  8. getText(Node node)
  9. getText(Node node)