Java tutorial
//package com.java2s; /******************************************************************************* * Copyright 2012-2015 eBay Software Foundation * This program is dual licensed under the MIT and Apache 2.0 licenses. * Please see LICENSE for more information. *******************************************************************************/ import java.util.ArrayList; import java.util.List; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class Main { private static List<String> findAllBeanIds(Element parent, String tag) { List<String> beanIds = new ArrayList<String>(); // First check to see if any parameters are null if (parent == null || tag == null) return null; // Check to see if this is the element we are interested in. This is // redundant apart from first call, but keep in to keep functionality // if (nodeNameEqualTo(parent, tag)) // return parent; // Get all the children NodeList list = parent.getChildNodes(); int listCount = list.getLength(); for (int k = 0; k < listCount; k++) { Node child = list.item(k); // If the node is not an element, ignore it if (child.getNodeType() != Node.ELEMENT_NODE) continue; // Check to see if this node is the node we want if (nodeNameEqualTo(child, tag)) { beanIds.add(((Element) child).getAttribute("id")); } } return beanIds; } /** * Check whether a name of given node is equal to a given name. Strips * namespace (if any). Case-sensitive. */ private static boolean nodeNameEqualTo(Node node, String target) { if (node == null || target == null) return false; String name = node.getNodeName(); // If target contains namespace, require exact match if (target.indexOf(':') < 0) { int index = name.indexOf(':'); if (index >= 0) name = name.substring(index + 1); // Strip namespace } return name.equals(target); } }