Java XML First Child Element getFirstChildElement(Element elm, String name)

Here you can find the source of getFirstChildElement(Element elm, String name)

Description

get First Child Element

License

EUPL

Declaration

public static Element getFirstChildElement(Element elm, String name) 

Method Source Code

//package com.java2s;
/*//from  w ww.j a v a  2s.c o m
 * Copyright 2004-2014 SmartBear Software
 *
 * Licensed under the EUPL, Version 1.1 or - as soon as they will be approved by the European Commission - subsequent
 * versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 *
 * http://ec.europa.eu/idabc/eupl
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the Licence is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the Licence for the specific language governing permissions and limitations
 * under the Licence.
*/

import org.w3c.dom.Element;

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

public class Main {
    public static Element getFirstChildElement(Element elm) {
        return getFirstChildElement(elm, null);
    }

    public static Element getFirstChildElement(Element elm, String name) {
        if (elm == null) {
            return null;
        }

        NodeList nl = elm.getChildNodes();
        for (int c = 0; c < nl.getLength(); c++) {
            Node node = nl.item(c);
            if (node.getNodeType() == Node.ELEMENT_NODE && (name == null || node.getNodeName().equals(name))) {
                return (Element) node;
            }
        }

        return null;
    }
}

Related

  1. getFirstChildElement(Element element)
  2. getFirstChildElement(Element element)
  3. getFirstChildElement(Element element)
  4. getFirstChildElement(Element element)
  5. getFirstChildElement(Element element, String namespaceURI, String tagName)
  6. getFirstChildElement(Element elm, String name)
  7. getFirstChildElement(Element elm, String name)
  8. getFirstChildElement(Element parent)
  9. getFirstChildElement(Element parent)