/** * Namespace-aware equals comparison. Returns <code>true</code> if either * {@link Node#getLocalName} or {@link Node#getNodeName} equals * <code>desiredName</code>, otherwise returns <code>false</code>. * * @param node (required) * @param desiredName (required) * @return */ public static boolean nodeNameEquals(final Node node, final String desiredName) { Validate.notNull(node, "Node must not be null"); Validate.notNull(desiredName, "Desired name must not be null"); return nodeNameMatch(node, desiredName); }
/** * Returns the first child element identified by its name. * * @param element the DOM element to analyze * @param childElementName the child element name to look for * @return the <code>org.w3c.dom.Element</code> instance, or * <code>null</code> if none found */ public static Element getChildElementByTagName(final Element element, final String childElementName) { Validate.notNull(element, "Element must not be null"); Validate.notNull(childElementName, "Element name must not be null"); final NodeList nl = element.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { final Node node = nl.item(i); if (node instanceof Element && nodeNameMatch(node, childElementName)) { return (Element) node; } } return null; }
/** * Retrieve all child elements of the given DOM element that match any of * the given element names. Only look at the direct child level of the given * element; do not go into further depth (in contrast to the DOM API's * <code>getElementsByTagName</code> method). * * @param element the DOM element to analyze * @param childElementNames the child element names to look for * @return a List of child <code>org.w3c.dom.Element</code> instances * @see org.w3c.dom.Element * @see org.w3c.dom.Element#getElementsByTagName */ public static List<Element> getChildElementsByTagName(final Element element, final String[] childElementNames) { Validate.notNull(element, "Element must not be null"); Validate.notNull(childElementNames, "Element names collection must not be null"); final List<String> childEleNameList = Arrays.asList(childElementNames); final NodeList nl = element.getChildNodes(); final List<Element> childEles = new ArrayList<Element>(); for (int i = 0; i < nl.getLength(); i++) { final Node node = nl.item(i); if (node instanceof Element && nodeNameMatch(node, childEleNameList)) { childEles.add((Element) node); } } return childEles; }