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

ListgetElements(Element topElm)
Gets the elements.
List<Element> retVals = new ArrayList<Element>();
NodeList childNodes = topElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
    Node n = childNodes.item(i);
    if (n.getNodeType() == Node.ELEMENT_NODE) {
        retVals.add((Element) n);
return retVals;
CollectiongetElements(final String elementName, final InputStream is, final boolean onlyValues)
get Elements
final Collection<String> elements = new ArrayList<>();
final ByteArrayOutputStream os = new ByteArrayOutputStream();
final XMLEventReader reader = XMLInputFactory.newInstance()
        .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name()));
final XMLEventWriter writer = XMLOutputFactory.newInstance()
        .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name()));
boolean read = false;
String characters = null;
...
ListgetElementsByName(Element elem, String s)
get all children of given type or empty list
List<Element> ret = new ArrayList<Element>();
for (Node child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (child instanceof Element) {
        if (child.getNodeName().equals(s)) {
            ret.add((Element) child);
return ret;
Element[]getElementsByTag(Element element, String tag)
get Elements By Tag
NodeList children = element.getChildNodes();
int size = children.getLength();
int elemCount = 0;
boolean wildCard = tag == null;
for (int i = 0; i < size; i++) {
    Node child = children.item(i);
    if (isElement(child) && (wildCard || tag.equals(((Element) child).getTagName()))) {
        elemCount++;
...
ListgetElementsByTagName(Element aElement, String aTagName)
get Elements By Tag Name
if (aElement == null) {
    return null;
List<Element> wElmts = new ArrayList<Element>();
NodeList wList = aElement.getElementsByTagName(aTagName);
for (int wI = 0; wI < wList.getLength(); wI++) {
    if (wList.item(wI).getNodeType() == Node.ELEMENT_NODE) {
        wElmts.add((Element) wList.item(wI));
...
ListgetElementsByTagName(Element element, String tag)
get single element by tag name.
List<Element> elems = new ArrayList<Element>();
NodeList list = element.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    Node node = list.item(i);
    if (node instanceof Element) {
        Element e = (Element) node;
        if (e.getTagName().equals(tag)) {
            elems.add(e);
...
ListgetElementsByTagName(Element element, String tagName)
get Elements By Tag Name
ArrayList<Element> children = new ArrayList<Element>();
if (element != null && tagName != null) {
    NodeList nodes = element.getElementsByTagName(tagName);
    for (int i = 0; i < nodes.getLength(); i++) {
        Node child = nodes.item(i);
        children.add((Element) child);
return children;
ArrayListgetElementsByTagName(Element node, String tagName)
Search the children of the argument node for named 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;
        if (e.getTagName().equals(tagName)) {
            list.add(e);
...
ListgetElementsByTagName(Element node, String xmlns, String nodeName)
Putting this in TestUtil and not XmlUtil since any production code could probably benefit by doing other work while iterating over NodeList directly, as opposed to looping once to construct this list we return, then iterating over it again to do such work.
List<Node> nodes = new ArrayList<Node>();
NodeList nodeList = node.getElementsByTagNameNS(xmlns, nodeName);
for (int i = 0; i < nodeList.getLength(); i++) {
    nodes.add(nodeList.item(i));
return nodes;
ListgetElementsByTagNameNS1(Element element, String nsName, String tagName)
This method searches children of Element element for element with tagName and namespaceURI nsName.
List list = new ArrayList();
if (element != null) {
    NodeList nl = element.getChildNodes();
    int length = nl.getLength();
    Node child = null;
    String childName;
    String childNS;
    for (int i = 0; i < length; i++) {
...