Java tutorial
//package com.java2s; /*-------------------------------------------------------------------------- * Copyright (c) 2004, 2006 OpenMethods, LLC * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Trip Gilman (OpenMethods), Lonnie G. Pryor (OpenMethods), * Vincent Pruitt (OpenMethods) * * T.D. Barnes (OpenMethods) - initial API and implementation -------------------------------------------------------------------------*/ 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 { public static List<Element> getElementsByTagNameNS(Element parent, String uri, String name, boolean localOnly) { List<Element> ret = new ArrayList<Element>(); if (!localOnly) { NodeList elementList = parent.getElementsByTagNameNS(uri, name); for (int i = 0; i < elementList.getLength(); i++) { ret.add((Element) elementList.item(i)); } } else { NodeList childList = parent.getChildNodes(); for (int i = 0; i < childList.getLength(); i++) { if (childList.item(i).getNodeType() != Node.ELEMENT_NODE) continue; Element child = (Element) childList.item(i); if ((uri.equals("*") || child.getNamespaceURI().equals(uri)) && ((child.getLocalName() == null && uri.equals("*") && child.getTagName().equals(name)) || child.getLocalName().equals(name))) ret.add(child); } } return ret; } }