Java XML First Child Element getFirstChildElement(Node start)

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

Description

gets the first child of a node which is an element.

License

BSD License

Declaration

public static Element getFirstChildElement(Node start) 

Method Source Code


//package com.java2s;
/*//from w  w  w. ja va 2  s  . c o  m
 * Copyright (c) 2012. betterFORM Project - http://www.betterform.de
 * Licensed under the terms of BSD License
 */

import org.w3c.dom.*;

public class Main {
    /**
     * gets the first child of a node which is an element. This avoids the whitespace problems when using
     * org.w3c.dom.node.getFirstChild(). Whitespace-nodes may also appear as children, but normally are not what you're
     * looking for.
     */
    public static Element getFirstChildElement(Node start) {
        Node n = null;
        NodeList nl = start.getChildNodes();
        int len = nl.getLength();

        if (len == 0) {
            return null;
        }

        for (int i = 0; i < len; i++) {
            n = nl.item(i);

            if (n.getNodeType() == Node.ELEMENT_NODE) {
                return ((Element) n);
            }
        }

        return null;
    }
}

Related

  1. getFirstChildElement(Node parent)
  2. getFirstChildElement(Node parent)
  3. getFirstChildElement(Node parent)
  4. getFirstChildElement(Node parent)
  5. getFirstChildElement(Node rootNode)
  6. getFirstChildElementByLocalName(Element parentElem, String localName)
  7. getFirstChildElementByName(Element parent, String tagName)
  8. getFirstChildElementByName(Element root, String tagName)
  9. getFirstChildElementByName(final Element parent, final String name)