Java XML Node Parent getFirstElementWithTagName(Node node, String element)

Here you can find the source of getFirstElementWithTagName(Node node, String element)

Description

Returns the first child element found with the specified tag name

License

Open Source License

Parameter

Parameter Description
node The node to search
element The element tag name to search for

Return

The matching element Node

Declaration

public synchronized static Node getFirstElementWithTagName(Node node, String element) 

Method Source Code


//package com.java2s;
import org.w3c.dom.*;

public class Main {
    /**//from w  w w .  j a va  2 s  .  c  o m
     *  Returns the first child element found with the specified tag name
     *  @param node The node to search
     *  @param element The element tag name to search for
     *  @return The matching element Node
     */
    public synchronized static Node getFirstElementWithTagName(Node node, String element) {
        if (node != null && element != null) {
            NodeList nl = node.getChildNodes();

            for (int i = 0; i < nl.getLength(); i++) {
                Node n = nl.item(i);

                if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(element))
                    return n;
            }
        }

        return null;
    }
}

Related

  1. getFirstElement(Node element)
  2. getFirstElement(Node node, String namespace, String name)
  3. getFirstElement(Node xml)
  4. getFirstElementByTagName(Node node, String tagName)
  5. getFirstElementByTagName(Node node, String tagName)
  6. getFirstNamedNode(Node node, String name)
  7. getFirstNodeByName(Node root, String tagName)
  8. getFirstValidNode(Node node)
  9. getParent(Element elem)