Java XML Element Find getAllElementsByTagName(Element elem, String name)

Here you can find the source of getAllElementsByTagName(Element elem, String name)

Description

Get a list of all child text Elements of given name directly under a given org.w3c.dom.Element .

License

Apache License

Parameter

Parameter Description
elem the parent Element
name the given name of searched child Elements

Return

a List of values of those child text Elements

Declaration

public static List<String> getAllElementsByTagName(Element elem, String name) 

Method Source Code

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

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 {
    /**/*from ww  w  .  j a  v a2 s  . c o m*/
     * Get a list of all child text Elements of given name directly
     * under a given {@code org.w3c.dom.Element}.
     *
     * @param elem the parent Element
     * @param name the given name of searched child Elements
     * @return a List of values of those child text Elements
     */
    public static List<String> getAllElementsByTagName(Element elem, String name) {
        NodeList nodeList = elem.getElementsByTagName(name);
        List<String> result = new ArrayList<String>();
        for (int i = 0; i < nodeList.getLength(); ++i) {
            NodeList children = nodeList.item(i).getChildNodes();
            if (children.getLength() == 0 || children.item(0).getNodeType() != Node.TEXT_NODE) {
                continue;
            }
            result.add(children.item(0).getNodeValue());
        }
        return result;
    }
}

Related

  1. findNodesByType(Element topElm, int type)
  2. findNodeValue(Element firstElement, String name)
  3. getAllElements(Element config, String elementName)
  4. getAllElements(Element element)
  5. getAllElements(Node context)
  6. getAllLeaveValues(Element element)
  7. getAllNodeNames(Element ele)
  8. getAllXmiIds(Element rootElement, String elementName)