Java XML Child Get by Name getChildElement(Element parentElement, String tagName)

Here you can find the source of getChildElement(Element parentElement, String tagName)

Description

This method returns the child element with the particular name.

License

Open Source License

Parameter

Parameter Description
parentElement The parent element.
tagName The child element name.

Return

The child element.

Declaration

public static Element getChildElement(Element parentElement, String tagName) 

Method Source Code

//package com.java2s;
/**/*w ww .  j av a2s .  c o m*/
 * Copyright 2003, 2004  ONCE Corporation
 *
 * LICENSE:
 * This file is part of BuilditMPI. It may be redistributed and/or modified
 * under the terms of the Common Public License, version 1.0.
 * You should have received a copy of the Common Public License along with this
 * software. See LICENSE.txt for details. Otherwise, you may find it online at:
 *   http://www.oncecorp.com/CPL10/ or http://opensource.org/licenses/cpl.php
 *
 * DISCLAIMER OF WARRANTIES AND LIABILITY:
 * THE SOFTWARE IS PROVIDED "AS IS".  THE AUTHOR MAKES NO REPRESENTATIONS OR
 * WARRANTIES, EITHER EXPRESS OR IMPLIED.  TO THE EXTENT NOT PROHIBITED BY LAW,
 * IN NO EVENT WILL THE AUTHOR BE LIABLE FOR ANY DAMAGES, INCLUDING WITHOUT
 * LIMITATION, LOST REVENUE, PROFITS OR DATA, OR FOR SPECIAL, INDIRECT,
 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS
 * OF THE THEORY OF LIABILITY, ARISING OUT OF OR RELATED TO ANY FURNISHING,
 * PRACTICING, MODIFYING OR ANY USE OF THE SOFTWARE, EVEN IF THE AUTHOR HAVE
 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * -----------------------------------------------------
 * $Id$
 */

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    /**
     * This method returns the child element with the particular name.
     * @param parentElement The parent element.
     * @param tagName The child element name.
     * @return The child element.
     */
    public static Element getChildElement(Element parentElement, String tagName) {
        NodeList nodes = parentElement.getElementsByTagName(tagName);
        if (nodes != null && nodes.getLength() != 0) {
            return (Element) nodes.item(0);
        } else {
            return null;
        }
    }
}

Related

  1. getChildElement(Element parent, String tagName)
  2. getChildElement(Element parent, String tagName)
  3. getChildElement(Element parent, String tagName)
  4. getChildElement(Element parentElement, String childElementName)
  5. getChildElement(Element parentElement, String name)
  6. getChildElement(Element root, String name)
  7. getChildElement(Element root, String name, boolean mandatory)
  8. getChildElement(final Element element, final String namespace, final String tagName)
  9. getChildElement(final Element parent, final String childName)