Java XML Child Get by Name getChildElements(Element element, String namespace, String localName)

Here you can find the source of getChildElements(Element element, String namespace, String localName)

Description

Returns all Element children of an Element that belong to the given namespace and have the given local name.

License

Apache License

Declaration

public static Element[] getChildElements(Element element, String namespace, String localName) 

Method Source Code

//package com.java2s;
/*// w w w  .java  2  s  .c om
 * Copyright 1999-2005 The Apache Software Foundation.
 * 
 * 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 java.util.ArrayList;

import org.w3c.dom.Element;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Main {
    /**
     * Returns all Element children of an Element that belong to the given
     * namespace.
     */
    public static Element[] getChildElements(Element element, String namespace) {
        ArrayList elements = new ArrayList();
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node instanceof Element && namespace.equals(node.getNamespaceURI()))
                elements.add(node);
        }
        return (Element[]) elements.toArray(new Element[elements.size()]);
    }

    /**
     * Returns all Element children of an Element that belong to the given
     * namespace and have the given local name.
     */
    public static Element[] getChildElements(Element element, String namespace, String localName) {
        ArrayList elements = new ArrayList();
        NodeList nodeList = element.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node instanceof Element && namespace.equals(node.getNamespaceURI())
                    && localName.equals(node.getLocalName())) {
                elements.add(node);
            }
        }
        return (Element[]) elements.toArray(new Element[elements.size()]);
    }
}

Related

  1. getChildElement(Node start, String name)
  2. getChildElement(NodeList childs, Node parent)
  3. getChildElement(String name, Element el)
  4. getChildElement(String name, Element elem)
  5. getChildElements(Element element, String childrenName)
  6. getChildElements(Element element, String tag)
  7. getChildElements(Element element, String tagName)
  8. getChildElements(Element p_rootElement, String p_elementName)
  9. getChildElements(Element parent, String elementTag)