Java XML First Child Element getFirstChildElement(Element root)

Here you can find the source of getFirstChildElement(Element root)

Description

getFirstChildElement purpose.

License

Open Source License

Parameter

Parameter Description
root The root element to look for children in.

Return

The element if a child was found, the null otherwise.

Declaration

public static Element getFirstChildElement(Element root) 

Method Source Code

//package com.java2s;
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved
 * (c) 2001 - 2013 OpenPlans/*w w w  . j  ava  2 s. c  om*/
 * This code is licensed under the GPL 2.0 license, available at the root
 * application directory.
 */

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

public class Main {
    /**
     * getFirstChildElement purpose.
     *
     * <p>
     * Used to help with XML manipulations. Returns the element which
     * represents the first child.
     * </p>
     *
     * @param root The root element to look for children in.
     *
     * @return The element if a child was found, the null otherwise.
     */
    public static Element getFirstChildElement(Element root) {
        Node child = root.getFirstChild();

        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE) {
                return (Element) child;
            }

            child = child.getNextSibling();
        }

        return null;
    }
}

Related

  1. getFirstChildElement(Element parent, String childName)
  2. getFirstChildElement(Element parent, String nsUri, String localPart)
  3. getFirstChildElement(Element parent, String tagName)
  4. getFirstChildElement(Element parent, String tagName)
  5. getFirstChildElement(Element parentElem)
  6. getFirstChildElement(final Node node)
  7. getFirstChildElement(final Node parent, final String elemName)
  8. getFirstChildElement(Node n)
  9. getFirstChildElement(Node n, String ns, String localName)