Java XML Node Find findNode(Node node, String nodeName)

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

Description

Find node by name recursively.

License

CDDL license

Parameter

Parameter Description
node Node
nodeName String

Return

Node

Declaration

public static Node findNode(Node node, String nodeName) 

Method Source Code

//package com.java2s;
/*// www  .j av  a 2  s  . c  o m
Copyright (c) 2013 eBay, Inc.
This program is licensed under the terms of the eBay Common Development and
Distribution License (CDDL) Version 1.0 (the "License") and any subsequent  version 
thereof released by eBay.  The then-current version of the License can be found 
at http://www.opensource.org/licenses/cddl1.php and in the eBaySDKLicense file that 
is under the eBay SDK ../docs directory.
*/

import org.w3c.dom.Node;

public class Main {
    /**
     * Find node by name recursively.
     * @param node Node
     * @param nodeName String
     * @return Node
     */
    public static Node findNode(Node node, String nodeName) {
        if (nodeName.equals(node.getNodeName()))
            return node;
        org.w3c.dom.NodeList children = node.getChildNodes();
        for (int i = 0; i < children.getLength(); i++) {
            Node ret = findNode(children.item(i), nodeName);
            if (ret != null)
                return ret;
        }
        return null;
    }
}

Related

  1. findElementNodeByName(Node node, String name)
  2. findElements(Node fromnode, String name)
  3. findElementsByName(Node node, List elements, String name)
  4. findNode(Document source, Element n)
  5. findNode(Node aNode, String aName)
  6. findNode(Node rootNode, String nodeName)
  7. findNodeByAttribute(Document document, String tagName, String attributeName, String attributeValue)
  8. findNodeByName(Document doc, String pathExpression)
  9. findNodeByTagName(Document document, String tagName)