Java XML Node Value getNodeValue(Node iNode)

Here you can find the source of getNodeValue(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

Returns the desired value of the node.

Declaration

public static String getNodeValue(Node iNode) 

Method Source Code

//package com.java2s;
/* Agrega es una federaci?n de repositorios de objetos digitales educativos formada por todas las Comunidades Aut?nomas propiedad de Red.es. Este c?digo ha sido desarrollado por la Entidad P?blica Empresarial red.es adscrita al Ministerio de Industria,Turismo y Comercio a trav?s de la Secretar?a de Estado de Telecomunicaciones y para la Sociedad de la Informaci?n, dentro del Programa Internet en el Aula, que se encuadra dentro de las actuaciones previstas en el Plan Avanza (Plan 2006-2010 para el desarrollo de la Sociedad de la Informaci?n y de Convergencia con Europa y entre Comunidades Aut?nomas y Ciudades Aut?nomas) y ha sido cofinanciado con fondos FEDER del Programa Operativo FEDER 2000-2006 ?Sociedad de la Informaci?n?
    /* www . j  a v  a2s  .c  o m*/
This program is free software: you can redistribute it and/or modify it under the terms of the European Union Public Licence (EUPL v.1.0).  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 European Union Public Licence (EUPL v.1.0). You should have received a copy of the EUPL licence along with this program.  If not, see http://ec.europa.eu/idabc/en/document/7330.
*/

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

public class Main {
    /**
     * This method gets the text node of an input node.
     *
     * @param iNode The node that contains the desired text node
     *
     * @return Returns the desired value of the node.
     */
    public static String getNodeValue(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. getNodeValue(final Node node)
  2. getNodeValue(List elements, String nodeName, String defaultValue)
  3. getNodeValue(Node aNode)
  4. getNodeValue(Node fatherNode, String nodeName)
  5. getNodeValue(Node iNode)
  6. getNodeValue(Node n)
  7. getNodeValue(Node N)
  8. getNodeValue(Node n)
  9. getNodeValue(Node node)