Java XML Node Find findElement(Node node, String tagName)

Here you can find the source of findElement(Node node, String tagName)

Description

find Element

License

Open Source License

Declaration

public static Element findElement(Node node, String tagName) 

Method Source Code

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

import org.w3c.dom.Element;

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

public class Main {
    public static Element findElement(Node node, String tagName) {
        Element result = null;//from www  . ja  v  a  2 s  .  c o m
        NodeList nodeList = node.getChildNodes();
        if (nodeList == null) {
            return result;
        }
        for (int i = 0; i < nodeList.getLength(); ++i) {
            Element element = checkIfElement(nodeList.item(i), tagName);
            if (element != null) {
                result = element;
                break;
            }
        }
        return result;
    }

    public static final Element checkIfElement(Node node) {
        Element result = null;
        if (isElement(node)) {
            result = (Element) node;
        }
        return result;
    }

    public static final Element checkIfElement(Node node, String tag) {
        Element result = null;
        if (isElement(node)) {
            Element tmp = (Element) node;
            if (tag == null || tmp.getTagName().equals(tag)) {
                result = tmp;
            }
        }
        return result;
    }

    public static boolean isElement(Node node) {
        return node.getNodeType() == Node.ELEMENT_NODE;
    }
}

Related

  1. findAllDescendantElements(Node e, String qname, Collection vector)
  2. findAllNodes(Node node, String[] nodeNames, ArrayList results)
  3. findAllNodesRecursive(Node n, String name)
  4. findAllProcessingIstructions(Node node, String name, List result)
  5. findAllUrisRecursive(Node node, List nodes)
  6. findElement(Node startNode, String name, String namespace)
  7. findElementById(Node head, String id)
  8. findElementById(Node node, String id)
  9. findElementByName(Node node, final String name)