Java XML First Child Element getFirstChild(Element element, String tag)

Here you can find the source of getFirstChild(Element element, String tag)

Description

get First Child

License

Open Source License

Declaration

public static Element getFirstChild(Element element, String tag) 

Method Source Code

//package com.java2s;
/*/*  w ww .  j  a v a 2 s  . c  o m*/
 * Copyright 2005-2015 by BerryWorks Software, LLC. All rights reserved.
 *
 * This file is part of EDIReader. You may obtain a license for its use directly from
 * BerryWorks Software, and you may also choose to use this software under the terms of the
 * GPL version 3. Other products in the EDIReader software suite are available only by licensing
 * with BerryWorks. Only those files bearing the GPL statement below are available under the GPL.
 *
 * EDIReader 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.
 *
 * EDIReader 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 EDIReader.  If not,
 * see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    public static Element getFirstChild(Element element, String tag) {
        Element result = null;
        NodeList childNodes = element.getChildNodes();
        for (int i = 0; i < childNodes.getLength(); i++) {
            Node node = childNodes.item(i);
            if (Node.ELEMENT_NODE == node.getNodeType() && node.getNodeName().equals(tag)) {
                result = (Element) node;
                break;
            }
        }
        return result;
    }
}

Related

  1. getFirstChild(Element element)
  2. getFirstChild(Element element)
  3. getFirstChild(Element element)
  4. getFirstChild(Element element, String child)
  5. getFirstChild(Element element, String namespaceUri, String localName)
  6. getFirstChild(Element parent, String childTagName)
  7. getFirstChild(Element parent, String name)
  8. getFirstChild(Element parent, String name)
  9. getFirstChild(Element root)