Java XML Child Get by Name getChildElementsByTagName(Element parentNode, String tagName)

Here you can find the source of getChildElementsByTagName(Element parentNode, String tagName)

Description

return all the child Elements under given Element parentNode and with the same tag name of tagName ; it's not deep search, so only the first generation children list is scanned
Note: the given tagName is just local name; namespace is not support right now.

License

Open Source License

Parameter

Parameter Description
parentNode a parameter
tagName a parameter

Exception

Parameter Description
DOMException an exception

Declaration

public static List<Element> getChildElementsByTagName(Element parentNode, String tagName) 

Method Source Code

//package com.java2s;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.DOMException;

import org.w3c.dom.Element;

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

public class Main {
    /**//from   w w  w.  j a  va2 s .  c  o m
     * return all the child Elements under given Element <code>parentNode</code> and with the same tag name of <code>tagName</code> ;
     * it's not deep search, so only the first generation children list is scanned <br/>Note: the given <code>tagName</code> is just local
     * name; namespace is not support right now.
     * 
     * @param parentNode
     * @param tagName
     * @return
     * @throws DOMException
     */
    public static List<Element> getChildElementsByTagName(Element parentNode, String tagName) {
        if (parentNode == null) {
            return null;
        }
        // if there is no tag name given, treat it as returning all the child Elements
        if (tagName == null || tagName.trim().length() == 0) {
            return getChildElements(parentNode);
        }
        List<Element> resultList = new ArrayList<Element>();
        NodeList childNodes = parentNode.getChildNodes();
        if (childNodes != null) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node node = childNodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE && node.getParentNode() == parentNode
                        && node.getLocalName().equals(tagName)) {
                    resultList.add((Element) node);
                }
            }
        }
        return resultList;
    }

    /**
     * return all the child Elements of given Element; it's not deep search, so only the first generation children list is scanned.
     * 
     * @param parentNode
     * @return an empty list if nothing found
     * @throws DOMException
     */
    public static List<Element> getChildElements(Element parentNode) throws DOMException {
        if (parentNode == null) {
            return null;
        }
        List<Element> resultList = new ArrayList<Element>();
        NodeList childNodes = parentNode.getChildNodes();
        if (childNodes != null) {
            for (int i = 0; i < childNodes.getLength(); i++) {
                Node node = childNodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE && node.getParentNode() == parentNode) {
                    resultList.add((Element) node);
                }
            }
        }
        return resultList;
    }
}

Related

  1. getChildElementsByTagName(Element elem, String name)
  2. getChildElementsByTagName(Element element, String name)
  3. getChildElementsByTagName(Element element, String tagName)
  4. getChildElementsByTagName(Element parentElement, String childTag)
  5. getChildElementsByTagName(Element parentElement, String name)
  6. getChildElementsByTagName(final Element element, final String tagName)
  7. getChildElementsByTagName(final Element parentElem, final String childName)
  8. getChildElementsByTagName(Node element, String tagName)
  9. getChildElementsByTagName(org.w3c.dom.Element xmlParent, String tagName)