Java XML Child Node Get getChildNode(Node parent, String tagName)

Here you can find the source of getChildNode(Node parent, String tagName)

Description

Get the first child with a certain tag name

License

Open Source License

Parameter

Parameter Description
parent a parameter
tagName a parameter

Exception

Parameter Description
Exception if no child with the given tag name is found

Return

child Node with the given tag name

Declaration

public static Node getChildNode(Node parent, String tagName)
        throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**/*w  w w .ja v a  2  s  .  co m*/
     * Get the first child with a certain tag name
     * 
     * @param parent
     * @param tagName
     * @return child Node with the given tag name
     * @throws Exception   if no child with the given tag name is found
     */
    public static Node getChildNode(Node parent, String tagName)
            throws Exception {
        NodeList children = parent.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            Node currentNode = children.item(i);
            if (currentNode.getLocalName().equalsIgnoreCase(tagName)) {
                return currentNode;
            }
        }

        throw new Exception("Child Node ['" + tagName + "'] not found.");

    }
}

Related

  1. getChildNode(Node node, String name)
  2. getChildNode(Node node, String name)
  3. getChildNode(Node node, String nodeName)
  4. getChildNode(Node node, String tag)
  5. getChildNode(Node parent, String childName)
  6. getChildNode(Node parentNode, String childElementName)
  7. getChildNode(Node root, String childName)
  8. getChildNode(Node rootNode, String childName)
  9. getChildNodeByLocalName(final Node parentNode, final String localNodeName)