Java XML Element Get by Name getElement(String elementName, Element parentElement)

Here you can find the source of getElement(String elementName, Element parentElement)

Description

Get the specified element from a parent element.

License

Open Source License

Parameter

Parameter Description
elementName the element tag to serach for.
parentElement the parent element.

Return

an element given the name and the parent element.

Declaration

public static Element getElement(String elementName, Element parentElement) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2002-2005 IBM Corporation and others.
 * 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://w  ww.  j  a v  a  2  s  .  c o  m
 *   IBM - Initial API and implementation
 *******************************************************************************/

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    /**
     * Get the specified element from a parent element.
     * 
     * @param elementName the element tag to serach for.
     * @param parentElement the parent element.
     * @return an element given the name and the parent element.
     */
    public static Element getElement(String elementName, Element parentElement) {
        Element returnElement = null;
        NodeList nl;

        // Get the list of elements
        if ((nl = parentElement.getElementsByTagName(elementName)) != null) {
            // Return first element found
            returnElement = (Element) nl.item(0);
        }

        // Return element
        return returnElement;
    }
}

Related

  1. getElement(Element parent, String localname, int position)
  2. getElement(Element parent, String tagName)
  3. getElement(Element parent, String tagName)
  4. getElement(Element parentElement, String nodeName)
  5. getElement(Node parent, int index)
  6. getElement(String name, int index, Element parent)
  7. getElementByName(Document document, String name)
  8. getElementByName(Element eParent, String elementName)
  9. getElementByName(Element parent, String nodeName)