Java Utililty Methods XML Element Find

List of utility methods to do XML Element Find

Description

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

Method

StringfindElementAsString(final Element e, final String find)
Find an element, return as a string.
final Element el = findElement(e, find);
if (el == null) {
    return null;
for (Node child = el.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (!(child instanceof Text)) {
        continue;
    return child.getNodeValue();
return null;
ElementfindElementWithId(String id, Element root)
fast search for Element with id attribute
if (id.equals(root.getAttribute("id")))
    return root;
NodeList list = root.getChildNodes();
int len = list.getLength();
for (int i = 0; i < len; i++) {
    Node n = list.item(i);
    if (n.getNodeType() != Node.ELEMENT_NODE)
        continue;
...
ListfindNodesByType(Element topElm, int type)
Finds element in DOM tree
List<Node> retvals = new ArrayList<Node>();
if (topElm == null)
    throw new IllegalArgumentException("topElm cannot be null");
synchronized (topElm.getOwnerDocument()) {
    Stack<Node> stack = new Stack<Node>();
    stack.push(topElm);
    while (!stack.isEmpty()) {
        Node curElm = stack.pop();
...
StringfindNodeValue(Element firstElement, String name)
find Node Value
String nodeValue = null;
NodeList firstNameList = firstElement.getElementsByTagName(name);
Element firstNameElement = (Element) firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
if (textFNList.getLength() > 0) {
    nodeValue = (textFNList.item(0)).getNodeValue();
return nodeValue;
...
ListgetAllElements(Element config, String elementName)
returns all the element with the given name
List<Element> out = new ArrayList<Element>();
NodeList children = config.getChildNodes();
for (int counter = 0; counter < children.getLength(); counter++) {
    if (children.item(counter) instanceof Element) {
        Element el = (Element) children.item(counter);
        if (el.getTagName().equals(elementName)) {
            out.add(el);
return out;
NodeListgetAllElements(Element element)
get All Elements
return element.getElementsByTagName("*");
Element[]getAllElements(Node context)
get All Elements
return getAllElements(context, "*");
ListgetAllElementsByTagName(Element elem, String name)
Get a list of all child text Elements of given name directly under a given org.w3c.dom.Element .
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());
...
ListgetAllLeaveValues(Element element)
get All Leave Values
if (element == null) {
    return null;
List<String> ret = new LinkedList<String>();
if (isLeaf(element)) {
    ret.add(element.getTextContent());
} else {
    NodeList nl = element.getChildNodes();
...
String[]getAllNodeNames(Element ele)
get All Node Names
NamedNodeMap atts = ele.getAttributes();
int nbrAtts = atts == null ? 0 : atts.getLength();
int nbrChildren = 0;
Set<String> childNames = null;
NodeList children = ele.getChildNodes();
if (children != null) {
    nbrChildren = children.getLength();
    childNames = new HashSet<String>();
...