Java XML First Child Element getDirectChildElementsByTag(Element node, String tag)

Here you can find the source of getDirectChildElementsByTag(Element node, String tag)

Description

This method returns a list of the direct element node children of this element node with the specified tag.

License

Open Source License

Parameter

Parameter Description
node - parent node
tag - tag of direct children to be returned

Return

a list containing the direct element children with the given tag

Declaration

public static List<Element> getDirectChildElementsByTag(Element node, String tag) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

import org.w3c.dom.*;

public class Main {
    private static final String DOM_WILDCARD = "*";

    /**/*from   w  w w. j ava2s  . c  o  m*/
     * This method returns a list of the direct element node children of this element node with the specified tag.
     * @param node - parent node
     * @param tag - tag of direct children to be returned
     * @return a list containing the direct element children with the given tag
     * @author Tristan Bepler
     */
    public static List<Element> getDirectChildElementsByTag(Element node, String tag) {
        List<Element> children = new ArrayList<Element>();
        Node child = node.getFirstChild();
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE
                    && (child.getNodeName().equals(tag) || tag.equals(DOM_WILDCARD))) {
                children.add((Element) child);
            }
            child = child.getNextSibling();
        }
        return children;
    }
}

Related

  1. getDirectChild(Node fNode, String localName, String namespace)
  2. getDirectChild(Node fNode, String localName, String namespace)
  3. getDirectChildElement(Element p_rootElement, String p_elementName)
  4. getDirectChildElements(Element parent)
  5. getDirectChildElements(Node fNode, String localName, String namespace)
  6. getDirectChildElementValue(Element p_rootElement, String p_elementName)
  7. getDirectChildElementValues(Element p_rootElement, String p_elementName)
  8. getDirectChildNamedElements( Element parent, String name)
  9. getDirectChildNode(Node node, String name)