Java tutorial
//package com.java2s; /* * XMLUtility.java 2/6/13 1:04 PM * * Copyright (C) 2012-2013 Nick Ma * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 3 * of the License, or any later version. * This program 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. */ import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Method selectNodeByName. * * @param node * @param nodeName * @return Node * <p/> * Purpose: given a node & tag name, returns a single node that matches. * Useful when it is known that a particular node will only have a single * node of a particular tag name. */ public static Node selectNodeByName(Node node, String nodeName) { Node nodeToReturn = null; int i = 0; // get all the child nodes NodeList currNodes = node.getChildNodes(); int length = currNodes.getLength(); // search for an occurence that matches nodeName provided while ((i < length) && (nodeToReturn == null)) { Node thisNode = currNodes.item(i); if (thisNode.getNodeName().equals(nodeName)) { nodeToReturn = thisNode; } i++; } return nodeToReturn; } }