Java XML Node Text Value getTextContent(Node baseNode)

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

Description

Get XML Node text content.

License

LGPL

Parameter

Parameter Description
baseNode The XML node from which the content is being retrieved.

Return

The text content of the XML node.

Declaration

public static String getTextContent(Node baseNode) 

Method Source Code

//package com.java2s;
/**// w  ww .j  a v a 2s .c  o m
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the latest version of the GNU Lesser General
 * Public License as published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program (LICENSE.txt); if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

import org.w3c.dom.Node;

public class Main {
    /**
     * Get XML <code>Node</code> text content.  This method duplicates the
     * org.w3c.dom.Node.getTextContent() method in JDK 1.5.
     *
     * @param baseNode The XML node from which the content is being retrieved.
     * @return The text content of the XML node.
     */
    public static String getTextContent(Node baseNode) {
        // if element, first child will be a text element with content
        Node child = baseNode.getFirstChild();
        if (child != null && child.getNodeType() == Node.TEXT_NODE) {
            return child.getNodeValue();
        }
        return "";
    }
}

Related

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