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

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

Description

get Element

License

Apache License

Declaration

public static Element getElement(Element parent, String tagName) 

Method Source Code

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

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

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

public class Main {
    public static Element getElement(Element parent, String tagName) {
        List<Element> children = getElements(parent, tagName);
        if (children.isEmpty()) {
            return null;
        }/*from   w w w. j a v  a  2 s .  c  o m*/
        return (Element) children.get(0);
    }

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

Related

  1. getElement(Document owner, String elementName, String nsURI)
  2. getElement(Document parent, String name)
  3. getElement(Element parent, String elementName)
  4. getElement(Element parent, String elementName)
  5. getElement(Element parent, String localname, int position)
  6. getElement(Element parent, String tagName)
  7. getElement(Element parentElement, String nodeName)
  8. getElement(Node parent, int index)
  9. getElement(String elementName, Element parentElement)