Java XML Child Node Get getChildNodeWithName(String name, Node n)

Here you can find the source of getChildNodeWithName(String name, Node n)

Description

looks through the child nodes of n and returns the first child with name

License

Open Source License

Parameter

Parameter Description
name the name of the child node to search for
n the node whose children we are searching

Return

the found node or null if the node is not found.

Declaration

public static Node getChildNodeWithName(String name, Node n) 

Method Source Code

//package com.java2s;
/*/*from w w  w . j  a  v  a2 s .co  m*/
 * Copyright (c) 2003-2010 The Regents of the University of California.
 * All rights reserved.
 *
 * '$Author: crawl $'
 * '$Date: 2013-02-21 11:20:05 -0800 (Thu, 21 Feb 2013) $' 
 * '$Revision: 31474 $'
 * 
 * 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 {
    /**
     * looks through the child nodes of n and returns the first child with name
     * 
     * @param name
     *            the name of the child node to search for
     * @param n
     *            the node whose children we are searching
     * @return the found node or null if the node is not found.
     */
    public static Node getChildNodeWithName(String name, Node n) {
        NodeList nl = n.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node c = nl.item(i);
            if (c.getNodeName().equals(name)) {
                return c;
            }
        }
        return null;
    }
}

Related

  1. getChildNodeValue(Node node, String elementName)
  2. getChildNodeValue(Node root, String childName, boolean emptyStringOnMissingChild)
  3. getChildNodeValue(String childNodeName, Node n)
  4. getChildNodeValueByName(Node parent, String nodeName)
  5. getChildNodeWithName(Node node, String name, String namespaceURI)
  6. getChildNodeWithTag(Node node, String nodeTag)