Here you can find the source of getElementNS(Element root, Set
Parameter | Description |
---|---|
root | an XML element whose children will be iterated, null values are allowed |
nsUris | a set of namespace URIs the wanted element can belong to |
wantedLocalName | local name of the wanted element |
null
when none found
public static Element getElementNS(Element root, Set<String> nsUris, String wantedLocalName)
//package com.java2s; /*//from w w w. ja v a2 s .c o m * Copyright 2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import org.w3c.dom.Element; import org.w3c.dom.Node; import java.util.Set; public class Main { /** * Searches for the first sub-element of the given XML element, which has * the given local name and whose namespace belongs to the given set. * * @param root * an XML element whose children will be iterated, null values are allowed * @param nsUris * a set of namespace URIs the wanted element can belong to * @param wantedLocalName * local name of the wanted element * @return * corresponding child element or <code>null</code> when none found */ public static Element getElementNS(Element root, Set<String> nsUris, String wantedLocalName) { if (root == null) { return null; } Node node = root.getFirstChild(); while (node != null) { if ((node instanceof Element) && nsUris.contains(node.getNamespaceURI()) && node.getLocalName().equals(wantedLocalName)) { return (Element) node; } node = node.getNextSibling(); } return null; } }