Java XML Child Get getChild(Element root, String... path)

Here you can find the source of getChild(Element root, String... path)

Description

get Child

License

Open Source License

Declaration

static Element getChild(Element root, String... path) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Objects;

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

public class Main {
    static Element getChild(Element root, String... path) {
        if (root == null || path.length == 0)
            return null;
        Element element = root;//w  ww  .  j a v  a2  s  .c  om
        for (String tagName : path) {
            if (element == null)
                return null;
            NodeList list = element.getChildNodes();
            if (list == null || list.getLength() == 0)
                return null;
            element = null;
            for (int i = 0; i < list.getLength(); i++) {
                Node node = list.item(i);
                if (!(node instanceof Element))
                    continue;
                Element child = (Element) node;
                if (Objects.equals(child.getLocalName(), tagName)) {
                    element = child;
                    break;
                }
            }
        }
        return element;
    }
}

Related

  1. getChild(Element parent, String element)
  2. getChild(Element parent, String name)
  3. getChild(Element parent, String name)
  4. getChild(Element parent, String name)
  5. getChild(Element parentEl, String name)
  6. getChild(final Element element, final String tagName)
  7. getChild(final Element parent, final String tag)
  8. getChild(final Element root, final String name)
  9. getChild(final Element root, final String name)