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

ListfindAllElementsByTagName(Element elem, String tagName)
find All Elements By Tag Name
List<Element> ret = new LinkedList<Element>();
findAllElementsByTagName(elem, tagName, ret);
return ret;
voidfindAllElementsByTagNameNS(Element el, String nameSpaceURI, String localName, List elementList)
find All Elements By Tag Name NS
if (localName.equals(el.getLocalName()) && nameSpaceURI.contains(el.getNamespaceURI())) {
    elementList.add(el);
Element elem = getFirstElement(el);
while (elem != null) {
    findAllElementsByTagNameNS(elem, nameSpaceURI, localName, elementList);
    elem = getNextElement(elem);
ElementfindComponentElement(Element root)
find Component Element
if (root == null) {
    return null;
NodeList children = root.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element trimmedElement = findComponentElement((Element) child);
...
ElementfindComponentElement(Element root)
find Component Element
if (root == null) {
    return null;
NodeList children = root.getChildNodes();
for (int i = 0; i < children.getLength(); i++) {
    Node child = children.item(i);
    if (child.getNodeType() == Node.ELEMENT_NODE) {
        Element trimmedElement = findComponentElement((Element) child);
...
ElementfindElement(Element element, String elementName)
If there is no such element, one will be created on element .
NodeList elements = element.getElementsByTagName(elementName);
if ((elements == null) || (elements.getLength() == 0)) {
    return createElement(element, elementName);
return (Element) elements.item(0);
ElementfindElement(Element element, String name)
find Element
NodeList list = element.getChildNodes();
for (int i = 0; i < list.getLength(); i++) {
    Node child = (Node) list.item(i);
    if (child instanceof Element) {
        Element childEl = (Element) child;
        if (name.equals(childEl.getLocalName())) {
            return childEl;
        } else {
...
ElementfindElement(Element root, String localName, String namespace)
find Element
return findElement(root, new QName(namespace, localName));
ElementfindElement(Element topElm, String localName, String namespace)
Find element.
Stack<Element> stack = new Stack<Element>();
stack.push(topElm);
while (!stack.isEmpty()) {
    Element curElm = stack.pop();
    if ((curElm.getLocalName().equals(localName))
            && (namespacesAreSame(curElm.getNamespaceURI(), namespace))) {
        return curElm;
    NodeList childNodes = curElm.getChildNodes();
    for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
        Node item = childNodes.item(i);
        if (item.getNodeType() == Node.ELEMENT_NODE) {
            stack.push((Element) item);
return null;
ElementfindElement(final Element e, final String find)
Find a child element.
for (Node child = e.getFirstChild(); child != null; child = child.getNextSibling()) {
    if (!(child instanceof Element)) {
        continue;
    final Element el = (Element) child;
    if (el.getNodeName().equals(find)) {
        return el;
return null;
ElementfindElement(final String idValue, final String idTagName, final String tagName, final Element root)
find element with tagName whose id tag is 'idTagName' and id value is 'id_value'
String textVal = null;
final NodeList nl = root.getElementsByTagName(tagName);
if (nl != null && nl.getLength() > 0) {
    for (int i = 0; i < nl.getLength(); i++) {
        final Element ei = (Element) nl.item(i);
        final NodeList n2 = ei.getElementsByTagName(idTagName);
        if (n2 != null && n2.getLength() > 0) {
            for (int j = 0; j < n2.getLength(); j++) {
...