Get First XML Child With Name - Java XML

Java examples for XML:DOM Node

Description

Get First XML Child With Name

Demo Code


//package com.java2s;

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

public class Main {
    static public Node GetFirstChildWithName(Node targetNode,
            String targetString) throws Exception {

        NodeList ChildNodeList = targetNode.getChildNodes();

        for (int i = 0; i < ChildNodeList.getLength(); i++) {

            if (ChildNodeList.item(i).getNodeName().equals(targetString)) {

                return ChildNodeList.item(i);

            }/*from   w ww .  ja va 2 s  .  co m*/

        }

        throw new Exception("Target node doesn't contain target string");

    }
}

Related Tutorials