Java XML Child Get by Name getChildElements(Element parent, String name)

Here you can find the source of getChildElements(Element parent, String name)

Description

Returns all the direct children of the parent that are Element nodes.

License

Open Source License

Parameter

Parameter Description
parent The parent.
name Only elements with this name will be included, or if "*", includes all child elements.

Declaration

public static List<Element> getChildElements(Element parent, String name) 

Method Source Code


//package com.java2s;

import org.w3c.dom.Element;
import org.w3c.dom.Node;

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**//from  www  .  j a  v a 2  s. c o  m
     * Returns all the direct children of the parent that are {@link Element}
     * nodes.
     *
     * @param parent The parent.
     * @param name   Only elements with this name will be included, or if "*",
     *               includes all child elements.
     */
    public static List<Element> getChildElements(Element parent, String name) {
        List<Element> kids = new ArrayList<Element>();
        for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
            if ((name.equals("*") || n.getNodeName().equals(name)) && n instanceof Element) {
                kids.add((Element) n);
            }
        }
        return kids;
    }
}

Related

  1. getChildElements(Element element, String tag)
  2. getChildElements(Element element, String tagName)
  3. getChildElements(Element p_rootElement, String p_elementName)
  4. getChildElements(Element parent, String elementTag)
  5. getChildElements(Element parent, String localName)
  6. getChildElements(Element parent, String name)
  7. getChildElements(Element parent, String name)
  8. getChildElements(Element parent, String nsUri, String localPart)
  9. getChildElements(Element parent, String nsUri, String localPart)