Java Utililty Methods XML Element Get

List of utility methods to do XML Element Get

Description

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

Method

intgetElementPosition(Element element)
Determines the index (starting at 0) of the given element relative to other element nodes for the same parent.
int num = -1;
Node node = element;
while (node != null) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        ++num;
    node = node.getPreviousSibling();
return num;
ListgetElements(Element aElement)
get Elements
if (aElement == null) {
    return null;
List<Element> wElmts = new ArrayList<Element>();
NodeList wList = aElement.getChildNodes();
for (int wI = 0; wI < wList.getLength(); wI++) {
    if (wList.item(wI).getNodeType() == Node.ELEMENT_NODE) {
        wElmts.add((Element) wList.item(wI));
...
ListgetElements(Element document, String elementName, String attrName, String attrValue)
Parses the document for a particular element containing the specified attribute
List<Object> choosenElements = new ArrayList<Object>();
NodeList elementList = document.getElementsByTagName(elementName);
for (int i = 0; i < elementList.getLength(); i++) {
    Map attr = getAttributes(elementList.item(i));
    String value = (String) attr.get(attrName);
    if (value != null && value.equalsIgnoreCase(attrValue)) {
        choosenElements.add(elementList.item(i));
return choosenElements;
ListgetElements(Element elem, String path)
Returns the list of named descendents.
List list = new ArrayList();
if (elem == null)
    return list;
int ndx = path.indexOf("/");
if (ndx != -1) {
    Element child = getElement(elem, path.substring(0, ndx));
    return getElements(child, path.substring(ndx + 1));
NodeList nl = elem.getChildNodes();
for (int i = 0, len = nl.getLength(); i < len; i++) {
    Node node = nl.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(path)) {
        list.add(node);
return list;
ListgetElements(Element element)
Returns all the direct child elements of the given element.
List<Element> elements = new ArrayList<>();
for (Node node : toIterable(element.getChildNodes())) {
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        elements.add((Element) node);
return elements;
ListgetElements(Element element, String tagName)
get Elements
final NodeList nodeList = element.getElementsByTagName(tagName);
return transElements(element, nodeList);
ArrayListgetElements(Element node)
Search the children of the argument node for elements.
ArrayList<Element> list = new ArrayList<Element>();
NodeList nl = node.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
    Node n = nl.item(i);
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        Element e = (Element) n;
        list.add(e);
return (list);
ArrayListgetElements(Element root)
Get all child elements of the specified (root) element.
if (root == null)
    throw new IllegalArgumentException("Null or invalid root element!");
NodeList lst = root.getChildNodes();
final int n = lst.getLength();
ArrayList<Element> a = new ArrayList<Element>(n);
for (int i = 0; i < n; i++) {
    Node node = lst.item(i);
    if (node.getNodeType() == Node.ELEMENT_NODE)
...
ListgetElements(Element root, String element)
get Elements
NodeList list = root.getElementsByTagName(element);
List<Element> ret = new ArrayList<>(list.getLength());
for (int i = 0; i < list.getLength(); i++) {
    ret.add((Element) list.item(i));
return ret;
Element[]getElements(Element root, String tagName)
get Elements
NodeList list = root.getElementsByTagName(tagName);
Element[] result = new Element[list.getLength()];
for (int i = 0; i < list.getLength(); i++) {
    result[i] = (Element) list.item(i);
return (result);