Java XML Element Get getElementNS(Element root, Set nsUris, String wantedLocalName)

Here you can find the source of getElementNS(Element root, Set nsUris, String wantedLocalName)

Description

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.

License

Apache License

Parameter

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

Return

corresponding child element or null when none found

Declaration

public static Element getElementNS(Element root, Set<String> nsUris, String wantedLocalName) 

Method Source Code


//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;
    }
}

Related

  1. getElementNameSansNamespace(Element element)
  2. getElementNamespaces(Element element, Set namespaces)
  3. getElementNamespaceURI(Element element)
  4. getElementNS(Element el, String nsuri, String name)
  5. getElementNS(Element elem, String namespaceUri, String localName)
  6. getElementPath(Element elem)
  7. getElementPath(Element element)
  8. getElementPath(Element element)
  9. getElementPosition(Element element)