Java XML Element Get by Name getElementByName(Element eParent, String elementName)

Here you can find the source of getElementByName(Element eParent, String elementName)

Description

Get an element from a parent element node

License

Open Source License

Parameter

Parameter Description
eParent a parameter
elementName a parameter

Declaration

public static Element getElementByName(Element eParent,
        String elementName) 

Method Source Code

//package com.java2s;
/*/* ww w .  jav a 2  s. c  om*/
 *      Copyright (c) 2004-2016 YAMJ Members
 *      https://github.com/orgs/YAMJ/people
 *
 *      This file is part of the Yet Another Movie Jukebox (YAMJ) project.
 *
 *      YAMJ is free software: you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation, either version 3 of the License, or
 *      any later version.
 *
 *      YAMJ is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with YAMJ.  If not, see <http://www.gnu.org/licenses/>.
 *
 *      Web: https://github.com/YAMJ/yamj-v2
 *
 */

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

public class Main {
    /**
     * Get an element from a parent element node
     *
     * @param eParent
     * @param elementName
     * @return
     */
    public static Element getElementByName(Element eParent,
            String elementName) {
        NodeList nlParent = eParent.getElementsByTagName(elementName);
        for (int looper = 0; looper < nlParent.getLength(); looper++) {
            if (nlParent.item(looper).getNodeType() == Node.ELEMENT_NODE) {
                return (Element) nlParent.item(looper);
            }
        }
        return null;
    }
}

Related

  1. getElement(Element parentElement, String nodeName)
  2. getElement(Node parent, int index)
  3. getElement(String elementName, Element parentElement)
  4. getElement(String name, int index, Element parent)
  5. getElementByName(Document document, String name)
  6. getElementByName(Element parent, String nodeName)
  7. getElementByTag(HTMLDocument htmlDoc, int offset, Tag tag)
  8. getElementByTagAndName(Document document, String tagName, String elementName)
  9. getElementByTagName(Document doc, String eleName)