get First Child By XML Tag Name - Java XML

Java examples for XML:XML Element Child

Description

get First Child By XML Tag Name

Demo Code


//package com.java2s;

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

public class Main {
    public static Node getFirstChildByTagName(Node parent, String tagName) {
        NodeList nodeList = parent.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE
                    && node.getNodeName().equalsIgnoreCase(tagName))
                return node;
        }/*w ww .  ja v a2s  .  c  om*/
        return null;
    }
}

Related Tutorials