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

Creative Commons 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;
/*******************************************************************************

 ADL SCORM 2004 4th Edition Sample Run-Time Environment

 The ADL SCORM 2004 4th Ed. Sample Run-Time Environment is licensed under
 Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States.

 The Advanced Distributed Learning Initiative allows you to:
 *  Share - to copy, distribute and transmit the work.
 *  Remix - to adapt the work. //from   www  .j  ava2 s . c o m

 Under the following conditions:
 *  Attribution. You must attribute the work in the manner specified by the author or
 licensor (but not in any way that suggests that they endorse you or your use
 of the work).
 *  Noncommercial. You may not use this work for commercial purposes. 
 *  Share Alike. If you alter, transform, or build upon this work, you may distribute
 the resulting work only under the same or similar license to this one. 

 For any reuse or distribution, you must make clear to others the license terms of this work. 

 Any of the above conditions can be waived if you get permission from the ADL Initiative. 
 Nothing in this license impairs or restricts the author's moral rights.

 *******************************************************************************/

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(final Node node)
  3. getNodeValue(List elements, String nodeName, String defaultValue)
  4. getNodeValue(Node aNode)
  5. getNodeValue(Node fatherNode, String nodeName)
  6. getNodeValue(Node iNode)
  7. getNodeValue(Node n)
  8. getNodeValue(Node N)
  9. getNodeValue(Node n)