Java XML Element Get getElementNS(Element elem, String namespaceUri, String localName)

Here you can find the source of getElementNS(Element elem, String namespaceUri, String localName)

Description

Get the first child with the specified name in the specified namespace.

License

Open Source License

Parameter

Parameter Description
elem Parent to get the child of
namespaceUri Namespace URI. Cannot be null (use getElement() to get no-namespace elements)
localName Local part of tagname.

Return

First element with specified name or null

Declaration

public static Element getElementNS(Element elem, String namespaceUri, String localName) 

Method Source Code

//package com.java2s;
/**/*from  w  w  w  .ja  va 2  s  .  c o  m*/
 * Copyright (c) 2009 DITA2InDesign project (dita2indesign.sourceforge.net)  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 org.w3c.dom.NodeList;

public class Main {
    /**
     * Get the first child with the specified name in the specified namespace.
     * @param elem Parent to get the child of
     * @param namespaceUri Namespace URI. Cannot be null (use getElement() to get no-namespace elements)
     * @param localName Local part of tagname.
     * @return First element with specified name or null
     */
    public static Element getElementNS(Element elem, String namespaceUri, String localName) {
        NodeList nl = elem.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node node = nl.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                if ((node.getNamespaceURI() != null && node.getNamespaceURI().equals(namespaceUri))
                        && node.getLocalName().equals(localName)) {
                    return (Element) node;
                }
            }
        }
        return null;
    }
}

Related

  1. getElementName(Object obj)
  2. getElementNameSansNamespace(Element element)
  3. getElementNamespaces(Element element, Set namespaces)
  4. getElementNamespaceURI(Element element)
  5. getElementNS(Element el, String nsuri, String name)
  6. getElementNS(Element root, Set nsUris, String wantedLocalName)
  7. getElementPath(Element elem)
  8. getElementPath(Element element)
  9. getElementPath(Element element)