Java XML Node Value getNodeValue(Node node)

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

Description

Searches parent node for matching child nodes, then collects and returns the first match node's value.

License

Open Source License

Parameter

Parameter Description
node The parent node

Return

The child node's value

Declaration

public static String getNodeValue(Node node) 

Method Source Code

//package com.java2s;
/*/*from   w  w  w. ja v a  2s .  c  o m*/
 * XMLUtility.java  2/6/13 1:04 PM
 *
 * Copyright (C) 2012-2013 Nick Ma
 *
 * This program 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 3
 * of the License, or 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.
 */

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

public class Main {
    /**
     * Searches parent node for matching child nodes, then collects and returns
     * the first match node's value. Useful when caller knows that there is only
     * one child node.
     * @param node     The parent node
     * @return The child node's value
     */
    public static String getNodeValue(Node node) {
        // sometimes jdom use more than one children nodes to represent text node 
        NodeList children = node.getChildNodes();
        String value = "";
        for (int i = 0; i < children.getLength(); i++) {
            value += children.item(i).getNodeValue();
        }
        return value;
    }

    /**
     * Searches parent node for matching child nodes, then collects and returns
     * the first match node's value. Useful when caller knows that there is only
     * one child node.
     *
     * @param node     The parent node
     * @param nodeName The child node element name
     * @return The child node's value
     */
    public static String getNodeValue(Node node, String nodeName) {
        Node n = getNode(node, nodeName);
        if (n == null) {
            return null;
        }
        Node ch = n.getFirstChild();
        if (ch != null) {
            return ch.getNodeValue();
        }
        return null;

    }

    /**
     * Searches parent node and returns first found matching node.
     *
     * @param node     The parent node
     * @param nodeName The child node's element name
     * @return The first matching child Node
     */
    public static Node getNode(Node node, String nodeName) {
        NodeList ch = node.getChildNodes();
        int l = ch.getLength();
        for (int i = 0; i < l; i++) {
            Node n = ch.item(i);
            if (n.getNodeName().equals(nodeName)) {
                return n;
            }
        }
        return null;
    }
}

Related

  1. getNodeValue(Node node)
  2. getNodeValue(Node node)
  3. getNodeValue(Node node)
  4. getNodeValue(Node node)
  5. getNodeValue(Node node)
  6. getNodeValue(Node node)
  7. getNodeValue(Node node)
  8. getNodeValue(Node node)
  9. getNodeValue(Node node)