Java Utililty Methods XML Child Get by Name

List of utility methods to do XML Child Get by Name

Description

The list of methods to do XML Child Get by Name are organized into topic(s).

Method

ListgetChildElementsByName(Element root, String tagName)
get Child Elements By Name
List<Element> tags = new LinkedList<Element>();
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node child = nl.item(i);
    if (child.getNodeType() != Node.ELEMENT_NODE) {
        continue;
    if (tagName.equals(child.getNodeName())) {
...
ListgetChildElementsByName(final Element parent, final String name)
Returns all elements in the parent that match the specified name.
List<Element> elements = new ArrayList<Element>();
if (parent != null) {
    Node child = parent.getFirstChild();
    while (child != null) {
        if (child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals(name)) {
            elements.add((Element) child);
        child = child.getNextSibling();
...
ListgetChildElementsByTagName(Element ele, String childEleName)
Retrieve all child elements of the given DOM element that match the given element name.
return getChildElementsByTagName(ele, new String[] { childEleName });
ListgetChildElementsByTagName(Element ele, String childEleName)
get Child Elements By Tag Name
NodeList nl = ele.getChildNodes();
List<Node> childEles = new ArrayList<Node>();
for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    if (node instanceof Element && childEleName.equals(node.getNodeName())
            || childEleName.equals(node.getLocalName())) {
        childEles.add(node);
return childEles;
ListgetChildElementsByTagName(Element ele, String childEleName, boolean localName)
Retrieve all child elements of the given DOM element that match the given element name.
NodeList nl = ele.getChildNodes();
List childEles = new ArrayList();
for (int i = 0; i < nl.getLength(); i++) {
    Node node = nl.item(i);
    String name = (localName) ? node.getLocalName() : node.getNodeName();
    if (node instanceof Element && childEleName.equals(name)) {
        childEles.add(node);
return childEles;
ListgetChildElementsByTagName(Element elem, String name)
get Child Elements By Tag Name
List<Element> result = new ArrayList<Element>();
NodeList nodeList = elem.getChildNodes();
for (int i = 0; i < nodeList.getLength(); i++) {
    Node child = nodeList.item(i);
    if (child.getNodeType() == Node.ELEMENT_NODE && child.getNodeName().equals(name)) {
        result.add((Element) child);
return result;
ListgetChildElementsByTagName(Element element, String name)
get Child Elements By Tag Name
List<Element> result = new ArrayList<Element>();
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
    Node node = childNodes.item(i);
    if (node instanceof Element) {
        String nodeName = node.getNodeName();
        if (nodeName != null) {
            if (nodeName.equals(name)) {
...
ListgetChildElementsByTagName(Element element, String tagName)
Gets the list of immediate child elements with the given tag name.
List<Element> elements = new ArrayList<Element>();
NodeList list = element.getChildNodes();
int size = list.getLength();
if (size > 0) {
    for (int i = 0; i < size; i++) {
        Node node = list.item(i);
        if (node instanceof Element) {
            Element e = (Element) node;
...
ListgetChildElementsByTagName(Element parentElement, String childTag)
get Child Elements By Tag Name
NodeList nodelist = parentElement.getChildNodes();
List<Element> nodes = new ArrayList<Element>();
for (int i = 0; i < nodelist.getLength(); i++) {
    Node temp = nodelist.item(i);
    if (temp.getNodeType() == Node.ELEMENT_NODE && temp.getNodeName().equals(childTag)) {
        nodes.add((Element) temp);
return nodes;
Element[]getChildElementsByTagName(Element parentElement, String name)
Retrieves all immediate child elements of the specified element whose names match the provided name parameter.
List children = new ArrayList();
NodeList nodes = parentElement.getChildNodes();
int length = nodes.getLength();
for (int index = 0; index < length; ++index) {
    if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE
            && name.equals(nodes.item(index).getNodeName())) {
        children.add(nodes.item(index));
Element[] childElements = new Element[children.size()];
return (Element[]) children.toArray(childElements);