Check if XML Element has Child - Java XML

Java examples for XML:XML Element Child

Description

Check if XML Element has Child

Demo Code


//package com.java2s;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    public static boolean hasChild(Element parent, String nodeName) {
        NodeList childNodes = parent.getChildNodes();
        int length = childNodes.getLength();
        for (int i = 0; i < length; i++)
            if (childNodes.item(i).getNodeName().equals(nodeName))
                return true;
        return false;
    }//from  www  .  j  av  a 2s  .  co m
}

Related Tutorials