Java XML Element Root getRootElement(Document d)

Here you can find the source of getRootElement(Document d)

Description

If first node found is not an element, returns null.

License

Open Source License

Parameter

Parameter Description
d a parameter

Return

the root element, or null if d is null or a root element is not found

Declaration

public static Element getRootElement(Document d) 

Method Source Code

//package com.java2s;
/**//from ww w . j av a 2  s  .c om
 * 
 * This file is part of SavoyCraft.
 * 
 * SavoyCraft is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 * 
 * SavoyCraft 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 Lesser General Public
 * License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with SavoyCraft. If not, see <http://www.gnu.org/licenses/>.
 * 
 */

import org.w3c.dom.Document;
import org.w3c.dom.Element;

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

public class Main {
    /**
     * If first node found is not an element, returns null.
     * @param d
     * @return the root element, or null if d is null or a root element is not
     *         found
     */
    public static Element getRootElement(Document d) {
        if (d == null) {
            return null;
        }

        NodeList list = d.getChildNodes();
        if (list.getLength() <= 0) {
            return null;
        } else {
            Node n = list.item(0);
            if (n instanceof Element) {
                return (Element) n;
            } else {
                return null;
            }
        }
    }
}

Related

  1. getRoot(Document doc)
  2. getRoot(Document doc)
  3. getRoot(Document document)
  4. getRoot(final Document doc)
  5. getRoot(SOAPElement e)
  6. getRootElement(Document doc)
  7. getRootElement(Document doc)
  8. getRootElement(Document doc)
  9. getRootElement(Document document)