Java XML Node Select selectNodeByName(Node node, String nodeName)

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

Description

Method selectNodeByName.

License

Open Source License

Parameter

Parameter Description
node a parameter
nodeName a parameter

Return

Node

Purpose: given a node & tag name, returns a single node that matches. Useful when it is known that a particular node will only have a single node of a particular tag name.

Declaration

public static Node selectNodeByName(Node node, String nodeName) 

Method Source Code

//package com.java2s;
/*/* www  .j  a v  a  2  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 {
    /**
     * Method selectNodeByName.
     *
     * @param node
     * @param nodeName
     * @return Node
     *         <p/>
     *         Purpose: given a node & tag name, returns a single node that matches.
     *         Useful when it is known that a particular node will only have a single
     *         node of a particular tag name.
     */
    public static Node selectNodeByName(Node node, String nodeName) {
        Node nodeToReturn = null;
        int i = 0;
        // get all the child nodes
        NodeList currNodes = node.getChildNodes();
        int length = currNodes.getLength();
        // search for an occurence that matches nodeName provided
        while ((i < length) && (nodeToReturn == null)) {
            Node thisNode = currNodes.item(i);
            if (thisNode.getNodeName().equals(nodeName)) {
                nodeToReturn = thisNode;
            }
            i++;
        }
        return nodeToReturn;
    }
}

Related

  1. selectElement(Node node, String uri, String nodeName)
  2. selectNode(Node node, String tag)
  3. selectNode(Node sibling, String uri, String nodeName, int number)
  4. selectNode(Node sibling, String uri, String nodeName, int number)
  5. selectNode(Node sibling, String uri, String nodeName, int number)
  6. selectNodes(Node sibling, String uri, String nodeName)
  7. selectNodes(Node sibling, String uri, String nodeName)
  8. selectNodeText(Node sibling, String uri, String nodeName, int number)
  9. selectNodeText(Node sibling, String uri, String nodeName, int number)