Java XML Child Get by Name getChildElement(Element parent, String tagName)

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

Description

get Child Element

License

Open Source License

Declaration

public static Element getChildElement(Element parent, String tagName) 

Method Source Code


//package com.java2s;

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

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

public class Main {
    public static Element getChildElement(Element parent, String tagName) {
        List<Element> elements = getElements(parent, tagName);
        return (elements.size() > 0) ? elements.get(0) : null;
    }//from   w  ww. ja v  a 2 s . c o  m

    public static List<Element> getElements(Element parent, String tagName) {
        List<Element> elements = new ArrayList<Element>();
        NodeList nodes = parent.getElementsByTagName(tagName);
        for (int i = 0; i < nodes.getLength(); i++) {
            Node node = nodes.item(i);
            if (node instanceof Element) {
                elements.add(Element.class.cast(node));
            }
        }
        return elements;
    }
}

Related

  1. getChildElement(Element parent, String name)
  2. getChildElement(Element parent, String name)
  3. getChildElement(Element parent, String tagName)
  4. getChildElement(Element parent, String tagName)
  5. getChildElement(Element parent, String tagName)
  6. getChildElement(Element parent, String tagName)
  7. getChildElement(Element parentElement, String childElementName)
  8. getChildElement(Element parentElement, String name)
  9. getChildElement(Element parentElement, String tagName)