Java tutorial
//package com.java2s; /* * $Id$ * * This code is derived from public domain sources. Commercial use is allowed. * However, all rights remain permanently assigned to the public domain. * * XMLUtils.java : A class of static XML utility methods. * * Copyright (C) 2009, 2010 Scott Herman * * This is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this code. If not, see <http://www.gnu.org/licenses/>. * */ import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Finds the node of the argument name in the tree under the argument node. * @param name The name of the node to search for, case insensitive. * @param node The root node of the tree under which to look for the named node. * @return the first found occurrence of the named node (breadth first), * or null if the node is not found. */ public static Node findChild(String name, Node node) { if (node == null) return null; if (node.hasChildNodes()) { NodeList kids = node.getChildNodes(); int numKids = kids.getLength(); for (int index = 0; index < numKids; ++index) { Node kid = kids.item(index); if (kid.getNodeName().equalsIgnoreCase(name)) return kid; } // for for (int index = 0; index < numKids; ++index) { Node found = findChild(name, kids.item(index)); if (found != null) return found; } // for } // if return null; } }