Java XML Child Node Get getChildNodeListByTagName(Element parent, String tagName)

Here you can find the source of getChildNodeListByTagName(Element parent, String tagName)

Description

get Child Node List By Tag Name

License

Apache License

Declaration

public static ArrayList<Element> getChildNodeListByTagName(Element parent, String tagName) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

import org.w3c.dom.Element;

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

public class Main {
    public static ArrayList<Element> getChildNodeListByTagName(Element parent, String tagName) {
        ArrayList<Element> lst = new ArrayList();
        traverseNodes(parent, tagName, lst);
        return lst;
    }//from   w w  w. j av  a  2 s.com

    private static void traverseNodes(Element parent, String tagName, List<Element> lst) {
        if (parent.getTagName().indexOf(tagName) != -1) {
            lst.add(parent);
            return;
        } else {
            NodeList nodeList = parent.getChildNodes();
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node item = nodeList.item(i);
                if (item.getNodeType() == Node.ELEMENT_NODE) {
                    traverseNodes((Element) item, tagName, lst);
                }
            }
        }
    }
}

Related

  1. getChildNodeByName(Node node, String childName)
  2. getChildNodeByName(Node node, String name)
  3. getChildNodeByTagName(Node node, String name)
  4. getChildNodeByType(Element element, short nodeType)
  5. getChildNodeGentle(Node node, String namespace, String localname)
  6. getChildNodes(Element ele)
  7. getChildNodes(final Node node)
  8. getChildNodes(final Node node, final short nodetype)
  9. getChildNodes(final Node node, final String attributeName, final String value)