Java XML Node Value getNodeValue(Node aNode)

Here you can find the source of getNodeValue(Node aNode)

Description

------------------------------------------------------------ Gets the String value of a node.

License

Open Source License

Parameter

Parameter Description
aNode Parent to search (should be the document root)

Return

Returns the value of the node --------------------------------------------------------------

Declaration

public static String getNodeValue(Node aNode) 

Method Source Code

//package com.java2s;
/*/*w  w w  .j  ava 2 s . c om*/
 * Copyright (c) 2010 The Regents of the University of California.
 * All rights reserved.
 *
 * '$Author: welker $'
 * '$Date: 2010-05-05 22:21:26 -0700 (Wed, 05 May 2010) $' 
 * '$Revision: 24234 $'
 * 
 * Permission is hereby granted, without written agreement and without
 * license or royalty fees, to use, copy, modify, and distribute this
 * software and its documentation for any purpose, provided that the above
 * copyright notice and the following two paragraphs appear in all copies
 * of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
 * ENHANCEMENTS, OR MODIFICATIONS.
 *
 */

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

public class Main {
    /**
     * ------------------------------------------------------------ Gets the
     * String value of a node. First checks it's value and if that is null then
     * it checks to see if it has a child node and gets the value of the first
     * child. Assumption: That the first child is a #text node, delibertly NOT
     * checking the first node's type
     * 
     * @param aNode
     *            Parent to search (should be the document root)
     * @return Returns the value of the node
     *         --------------------------------------------------------------
     */
    public static String getNodeValue(Node aNode) {
        String value = null;
        if (aNode.getNodeValue() != null) {
            value = aNode.getNodeValue() != null ? aNode.getNodeValue()
                    .trim() : null;
        } else {
            NodeList list = aNode.getChildNodes();
            if (list.getLength() == 1) {
                Node child = list.item(0);
                if (child != null) {
                    value = child.getNodeValue() != null ? child
                            .getNodeValue().trim() : null;
                }
            }
        }
        return value;
    }
}

Related

  1. getNodeVal(Node node)
  2. getNodeValue(final Node iNode)
  3. getNodeValue(final Node node)
  4. getNodeValue(final Node node)
  5. getNodeValue(List elements, String nodeName, String defaultValue)
  6. getNodeValue(Node fatherNode, String nodeName)
  7. getNodeValue(Node iNode)
  8. getNodeValue(Node iNode)
  9. getNodeValue(Node N)