Retrieves the first XML element, within the supplied element that has the specified tagName. - Android XML

Android examples for XML:XML Element

Description

Retrieves the first XML element, within the supplied element that has the specified tagName.

Demo Code

/**/*from  w  w w . j ava2s  .  c om*/
 * RAMPART - Robust Automatic MultiPle AssembleR Toolkit
 * Copyright (C) 2013  Daniel Mapleson - TGAC
 *
 * This program 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
 * (at your option) any later version.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 **/
//package com.java2s;
import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

public class Main {
    /**
     * Retrieves the first element, within the supplied element that has the specified tagName.
     * @param ele Parent element to search through
     * @param tagName Element name to find
     * @return Element within the parent element that has the specfied name, or null.
     */
    public static Element getDistinctElementByName(Element ele,
            String tagName) {
        NodeList nl = ele.getElementsByTagName(tagName);
        if (nl != null && nl.getLength() > 0) {
            return (Element) nl.item(0);
        }

        return null;
    }
}

Related Tutorials