Java XML First Child Element firstChildElement(Element element, String childElementName)

Here you can find the source of firstChildElement(Element element, String childElementName)

Description

Return the first child Element with the given name; if name is null returns the first element.

License

Open Source License

Declaration

public static Element firstChildElement(Element element, String childElementName) 

Method Source Code

//package com.java2s;
/*// ww  w .  j  a v  a  2  s .  c  o  m
 * XMLUtils.java
 *
 * Copyright (c) 1998 - 2006 BusinessTechnology, Ltd.
 * All rights reserved
 *
 * This program is the proprietary and confidential information
 * of BusinessTechnology, Ltd. and may be used and disclosed only
 * as authorized in a license agreement authorizing and
 * controlling such use and disclosure
 *
 * Millennium ERP system.
 *
 */

import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class Main {
    /**
     * Return the first child Element with the given name; if name is null returns the first element.
     */
    public static Element firstChildElement(Element element, String childElementName) {
        if (element == null)
            return null;
        // get the first element with the given name
        Node node = element.getFirstChild();

        if (node != null) {
            do {
                if (node.getNodeType() == Node.ELEMENT_NODE
                        && (childElementName == null || childElementName.equals(node.getNodeName()))) {
                    Element childElement = (Element) node;

                    return childElement;
                }
            } while ((node = node.getNextSibling()) != null);
        }
        return null;
    }

    /**
     * Return the first child Element with the given name; if name is null returns the first element.
     */
    public static Element firstChildElement(Element element, String childElementName, String attrName,
            String attrValue) {
        if (element == null)
            return null;
        // get the first element with the given name
        Node node = element.getFirstChild();

        if (node != null) {
            do {
                if (node.getNodeType() == Node.ELEMENT_NODE
                        && (childElementName == null || childElementName.equals(node.getNodeName()))) {
                    Element childElement = (Element) node;

                    String value = childElement.getAttribute(attrName);

                    if (value != null && value.equals(attrValue)) {
                        return childElement;
                    }
                }
            } while ((node = node.getNextSibling()) != null);
        }
        return null;
    }
}

Related

  1. extractFirstChildValue(final Node node)
  2. firstByClass(NodeList childList, String className)
  3. firstChild(Element element, String name)
  4. firstChild(Element element, String name)
  5. firstChildByTagName(Node root, String tagName)
  6. firstChildElement(Element parent)
  7. firstChildElement(Node node)
  8. firstChildElement(Node node)
  9. firstChildNodeWithName(org.w3c.dom.Node node, String name)