Java XML First Child Element getFirstChildTag(Element el, String tag)

Here you can find the source of getFirstChildTag(Element el, String tag)

Description

Method getFirstChildTag.

License

Apache License

Parameter

Parameter Description
el Element where to get children from
tag Tag to match

Return

Element The element found, or null if no match found.

Declaration

static public Element getFirstChildTag(Element el, String tag) 

Method Source Code

//package com.java2s;
/*//from   w  w w. j  a va 2  s . c  o  m
   Copyright 2013, 2016 Nationale-Nederlanden
    
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

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

public class Main {
    /**
     * Method getFirstChildTag. Return the first child-node which is an element
     * with tagName equal to given tag.
     * This method only looks at the direct children of the given node, and
     * doesn't descent deeper into the tree.
     *
     * @param el       Element where to get children from
     * @param tag      Tag to match
     * @return Element The element found, or <code>null</code> if no match
     *                  found.
     */
    static public Element getFirstChildTag(Element el, String tag) {
        NodeList nl;
        int len;

        nl = el.getChildNodes();
        len = nl.getLength();
        for (int i = 0; i < len; ++i) {
            Node n = nl.item(i);
            if (n instanceof Element) {
                Element elem = (Element) n;
                if (elem.getTagName().equals(tag)) {
                    return elem;
                }
            }
        }
        return null;
    }
}

Related

  1. getFirstChildNamed(Node node, String name)
  2. getFirstChildNode(Node parentNode, String childNodeName)
  3. getFirstChildNodeWithName(String nodeName, Node parentNode)
  4. getFirstChildOfTagName(Element elem, String name)
  5. getFirstChildOfType(final Element elParent, final String childTag)
  6. getFirstChildText(@Nullable final Node aStartNode)
  7. getFirstChildText(Node aNode)
  8. getFirstChildText(Node parent)
  9. getFirstChildTextByTagName(Element element, String tagName)