Java XML Node Name getSubNodeValue(Node node, String nodeName, String subNodeName)

Here you can find the source of getSubNodeValue(Node node, String nodeName, String subNodeName)

Description

Searches parent node 2 levels deep and returns the first match node's value.

License

Open Source License

Parameter

Parameter Description
node The parent node
nodeName The 1st level child node element name
subNodeName The 2nd level child node element name

Return

The child node's value

Declaration

public static String getSubNodeValue(Node node, String nodeName, String subNodeName) 

Method Source Code

//package com.java2s;
/*/*from   ww  w .  j  a va2  s. c om*/
 * 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 2 levels deep and returns the first match node's
     * value. Useful when caller knows that there is a matching node 2 levels
     * deep.
     *
     * @param node        The parent node
     * @param nodeName    The 1st level child node element name
     * @param subNodeName The 2nd level child node element name
     * @return The child node's value
     */
    public static String getSubNodeValue(Node node, String nodeName, String subNodeName) {
        Node subNode = getNode(node, nodeName);
        if (subNode == null) {
            return null;
        }
        return getNodeValue(subNode, subNodeName);
    }

    /**
     * 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;
    }

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

    }
}

Related

  1. getNodes(Node iNode, String iNodeName)
  2. getNodes(Node module, String interfaceName, String nodeName)
  3. getNodes(Node node, String nodeName)
  4. getSimpleName(final Node node)
  5. getSubNodes(Node node, String subNodeName)
  6. getTagName(Node labelNode)
  7. getTagName(Node node)
  8. getTagName(Node tag, boolean useJsfcTags)
  9. getTagNameWithoutPrefix(Node node)