Java XML Node Text Value getText(Node n)

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

Description

Gets the text for a node by concatenating child TEXT elements.

License

Open Source License

Declaration

public synchronized static String getText(Node n) 

Method Source Code


//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**/*from   w ww. j a  v  a2  s.c o  m*/
     *   Gets the text for a node by concatenating child TEXT elements.
     */
    public synchronized static String getText(Node n) {
        if (n == null)
            throw new IllegalArgumentException("Node argument cannot be null");

        StringBuilder b = new StringBuilder();
        NodeList nl = n.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            if (nl.item(i).getNodeType() == Node.TEXT_NODE)
                b.append(nl.item(i).getNodeValue());
            else if (nl.item(i).getNodeType() == Node.CDATA_SECTION_NODE)
                b.append(nl.item(i).getNodeValue());
        }

        return b.toString().trim();
    }
}

Related

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