Java XML Node Value getNodeValue(final Node iNode)

Here you can find the source of getNodeValue(final Node iNode)

Description

This method gets the text node of an input node.

License

Open Source License

Parameter

Parameter Description
iNode : The node that contains the desired text node

Return

The desired value of the node.

Declaration

public static String getNodeValue(final Node iNode) 

Method Source Code

//package com.java2s;
/*/*from  w w  w. j a  va2s. co  m*/
 * Copyright (c) [yyyy] [TITULAR]
 * This file is part of [SSSSS].  
 * 
 * [SSSS] is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 2 of the License, or 
 * (at your option) any later version.
 * 
 * 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 
 * General Public License for more details, currently published 
 * at http://www.gnu.org/copyleft/gpl.html or in the gpl.txt in 
 * the root folder of this distribution.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301, USA.  
 */

import org.w3c.dom.*;

public class Main {
    /**
     * This method gets the text node of an input node.
     *
     * @param iNode : The node that contains the desired text node
     *
     * @return The desired value of the node.
     */
    public static String getNodeValue(final Node iNode) {
        // Create a string to hold the results.
        String value = new String();

        // Check to make sure node is not null
        if (iNode != null) {
            // Get a list of the children of the input node
            NodeList children = iNode.getChildNodes();
            int numChildren = children.getLength();

            // Cycle through all children of node to get the text
            if (children != null) {
                for (int i = 0; i < numChildren; i++) {
                    // make sure we have a text element
                    if ((children.item(i).getNodeType() == Node.TEXT_NODE)
                            || (children.item(i).getNodeType() == Node.CDATA_SECTION_NODE)) {

                        value = value + children.item(i).getNodeValue().trim();
                    }
                } // end looping over the children nodes
            }
        }

        // Return the value of the node.
        return value;
    }
}

Related

  1. getNodeVal(Node node)
  2. getNodeValue(final Node node)
  3. getNodeValue(final Node node)
  4. getNodeValue(List elements, String nodeName, String defaultValue)
  5. getNodeValue(Node aNode)