get First XML Child By Type - Java XML

Java examples for XML:XML Element Child

Description

get First XML Child By Type

Demo Code

// This library is free software; you can redistribute it and/or
//package com.java2s;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    public static Node getFirstChildByType(Element element, int nodeType) {
        NodeList children = element.getChildNodes();
        int childCount = children.getLength();

        for (int i = 0; i < childCount; i++) {
            Node child = children.item(i);
            if (child.getNodeType() == nodeType) {
                return child;
            }/*from  w w w  .ja  v  a  2 s .com*/
        }

        return null;
    }
}

Related Tutorials