Java XML First Child Element getFirstChildElementsByTagName(Node contextNode, String elementName)

Here you can find the source of getFirstChildElementsByTagName(Node contextNode, String elementName)

Description

Returns list of the first child element retrieved by tag name from the parent node and null otherwise.

License

Open Source License

Parameter

Parameter Description
parentNode parent node.
elementName element name to found.

Return

list of the first child element

Declaration

public static Collection<Element> getFirstChildElementsByTagName(Node contextNode, String elementName) 

Method Source Code

//package com.java2s;
/**/*from   w  ww .  ja  va2 s .  c  o m*/
 *  Copyright (c) 2013-2014 Angelo ZERR.
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the Eclipse Public License v1.0
 *  which accompanies this distribution, and is available at
 *  http://www.eclipse.org/legal/epl-v10.html
 *
 *  Contributors:
 *  Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

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

public class Main {
    /**
     * Returns list of the first child element retrieved by tag name from the
     * parent node and null otherwise.
     * 
     * @param parentNode
     *            parent node.
     * @param elementName
     *            element name to found.
     * @return list of the first child element
     */
    public static Collection<Element> getFirstChildElementsByTagName(Node contextNode, String elementName) {
        Collection<Element> elements = null;
        Element result = null;

        if (contextNode.getNodeType() == Node.DOCUMENT_NODE) {
            result = ((Document) contextNode).getDocumentElement();
            if (!result.getNodeName().equals(elementName)) {
                result = null;
            }
        } else {
            NodeList nodes = contextNode.getChildNodes();
            Node node;
            for (int i = 0; i < nodes.getLength(); i++) {
                node = nodes.item(i);
                if (node.getNodeType() == Node.ELEMENT_NODE && node.getNodeName().equals(elementName)) {
                    if (elements == null) {
                        elements = new ArrayList<Element>();
                    }
                    result = (Element) node;
                    elements.add(result);
                }
            }
        }
        if (elements == null) {
            return Collections.emptyList();
        }
        return elements;
    }
}

Related

  1. getFirstChildElementNS(Element elm, String tns, String localName)
  2. getFirstChildElementNS(Node parent, String uri)
  3. getFirstChildElementNS(Node parent, String[][] elemNames)
  4. getFirstChildElementOfName(@Nonnull final Node aStartNode, @Nullable final String sName)
  5. getFirstChildElementsByTagName( Node contextNode, String elementName)
  6. getFirstChildElementWithName(Element elem, String name)
  7. getFirstChildElmtByTag(String aTagName)
  8. getFirstChildElmtByTagAndAttribut(Node aNode, String aTagName, String aAttrId, String aAttrValue)
  9. getFirstChildNamed(Element elem, String childName)