Java XML Child Get by Name getChildElemByTagName(Element parent, String tagName)

Here you can find the source of getChildElemByTagName(Element parent, String tagName)

Description

returns first child element with fitting tag name.

License

Common Public License

Declaration

public static Element getChildElemByTagName(Element parent, String tagName) 

Method Source Code

//package com.java2s;
/*/*from   w ww. j  a  v a2 s .  com*/
 * ====================================================================
 * This software is subject to the terms of the Common Public License
 * Agreement, available at the following URL:
 *   http://www.opensource.org/licenses/cpl.html .
 * Copyright (C) 2003-2004 TONBELLER AG.
 * All Rights Reserved.
 * You must accept the terms of that agreement to use this software.
 * ====================================================================
 *
 * 
 */

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

public class Main {
    /**
     * returns first child element with fitting tag name.
     */
    public static Element getChildElemByTagName(Element parent, String tagName) {
        NodeList children = parent.getChildNodes();
        for (int i = 0; i < children.getLength(); ++i) {
            if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
                Element child = (Element) children.item(i);
                if (child.getTagName().equals(tagName))
                    return child;
            }
        }
        return null;
    }
}

Related

  1. getChildByTagName(Element element, String tagName)
  2. getChildByTagName(Node parent, String tagname)
  3. getChildData(Element e, String tag)
  4. getChildDouble(final Element parent, final String name)
  5. getChildDoubleByName(Node node, String nodeName, double errValue)
  6. getChildElement(Element currentNode, String nsURI, String localName)
  7. getChildElement(Element el, String tagName)
  8. getChildElement(Element el, String tagName)
  9. getChildElement(Element ele, String childName)