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

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

Description

Get the node set from parent node by the specified name

License

Apache License

Parameter

Parameter Description
parent a parameter
name a parameter

Declaration

public static Element[] getElementsByName(Element parent, String name) 

Method Source Code

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

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

import java.util.ArrayList;

public class Main {
    /**/* www  .  ja  v  a 2 s. c  om*/
     * Get the node set from parent node by the specified  name
     *
     * @param parent
     * @param name
     * @return
     */
    public static Element[] getElementsByName(Element parent, String name) {
        ArrayList resList = new ArrayList();
        NodeList nl = getNodeList(parent);
        for (int i = 0; i < nl.getLength(); i++) {
            Node nd = nl.item(i);
            if (nd.getNodeName().equals(name)) {
                resList.add(nd);
            }
        }
        Element[] res = new Element[resList.size()];
        for (int i = 0; i < resList.size(); i++) {
            res[0] = (Element) resList.get(i);
        }
        return res;
    }

    /**
     * Get all child nodes of a parent node
     *
     * @param parent
     * @return
     */
    public static NodeList getNodeList(Element parent) {
        return parent.getChildNodes();
    }
}

Related

  1. getElements(Element parent, String tagName)
  2. getElements(Element parent, String tagName)
  3. getElements(Element parent, String tagName)
  4. getElements(Element parentElement, String nodeName)
  5. getElements(final Element parent, final String name)
  6. getElementsByTagName(Document doc, String tagName)
  7. getElementsByTagName(Element parent, String name, boolean localOnly)
  8. getElementsByTagName(Element parent, String tagName)
  9. getElementsByTagName(final Element parentElement, final String elementName)