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 java.util.ArrayList; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { /** * Method allNodesByName. * * @param node * @param nodeName * @return ArrayList * <p/> * Purpose: given a node & tag name, returns an ArrayList of ALL nodes that * have that tag. Useful when there are multiple nodes of the same. For * example, used to parse an entire XML document into an array of testcases. */ public static ArrayList allNodesByName(Node node, String nodeName) { ArrayList nodes = new ArrayList(); NodeList currNodes = node.getChildNodes(); int length = currNodes.getLength(); for (int i = 0; i < length; i++) { Node thisNode = currNodes.item(i); if (thisNode.getNodeName().equals(nodeName)) { nodes.add(thisNode); } } if (nodes.size() > 0) { return nodes; } else { return null; } } }