Java XML First Child Element getFirstChildElement(Node node)

Here you can find the source of getFirstChildElement(Node node)

Description

Get a first XML element of a given node.

License

BSD License

Parameter

Parameter Description
node the pareent XML node

Return

Node the first child of a given XML node.

Declaration


public static Node getFirstChildElement(Node node) 

Method Source Code

//package com.java2s;
/*L/*from w  w  w .j ava  2 s .  co  m*/
 * Copyright SAIC, SAIC-Frederick.
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/caadapter/LICENSE.txt for details.
 */

import org.w3c.dom.Node;

public class Main {
    /**
     * Get a first XML element of a given node. For example, with the following structure
     *   <node1>
     *     <node2>
     *   getFirstChildElement(node1) will return a reference to <node2>
     *
     * @param node the pareent XML node
     * @return Node the first child of a given XML node.
     */

    public static Node getFirstChildElement(Node node) {
        if (node == null)
            return null;
        Node child = node.getFirstChild();
        if (child == null)
            return null;
        if (child.getNodeType() == Node.ELEMENT_NODE)
            return child;
        while (child != null) {
            if (child.getNodeType() == Node.ELEMENT_NODE)
                return child;
            child = child.getNextSibling();
        }
        return null;
    }
}

Related

  1. getFirstChildElement(Node node)
  2. getFirstChildElement(Node node)
  3. getFirstChildElement(Node node)
  4. getFirstChildElement(Node node)
  5. getFirstChildElement(Node node)
  6. getFirstChildElement(Node node)
  7. getFirstChildElement(Node node)
  8. getFirstChildElement(Node node, String tag)
  9. getFirstChildElement(Node parent)