Java XML Element Get getElements(Element element)

Here you can find the source of getElements(Element element)

Description

Returns all the direct child elements of the given element.

License

Apache License

Declaration

public static List<Element> getElements(Element element) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License"); you may not

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 {
    /** Returns all the direct child elements of the given element. */
    public static List<Element> getElements(Element element) {
        List<Element> elements = new ArrayList<>();
        for (Node node : toIterable(element.getChildNodes())) {
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                elements.add((Element) node);
            }//from w w w .j  av a2 s .  c  o  m
        }
        return elements;
    }

    /** Converts a NodeList to an Iterable of Nodes. */
    public static Iterable<Node> toIterable(NodeList nodeList) {
        List<Node> nodes = new ArrayList<>(nodeList.getLength());
        for (int i = 0; i < nodeList.getLength(); i++) {
            nodes.add(nodeList.item(i));
        }
        return nodes;
    }
}

Related

  1. getElementPath(Element element)
  2. getElementPosition(Element element)
  3. getElements(Element aElement)
  4. getElements(Element document, String elementName, String attrName, String attrValue)
  5. getElements(Element elem, String path)
  6. getElements(Element element, String tagName)
  7. getElements(Element node)
  8. getElements(Element root)
  9. getElements(Element root, String element)