Java XML Element Root getRootElement(Document parent)

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

Description

Gets the root element of a document.

License

Open Source License

Parameter

Parameter Description
parent the document

Return

the root element

Declaration

public static Element getRootElement(Document parent) 

Method Source Code

//package com.java2s;

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

public class Main {
    /**//from   w  w w  .j av a2  s.com
     * Gets the root element of a document.
     * @param parent the document
     * @return the root element
     */
    public static Element getRootElement(Document parent) {
        return getFirstChildElement((Node) parent);
    }

    /**
     * Gets the first child element of an element.
     * @param parent the parent element
     * @return the first child element or null if there are no child elements
     */
    public static Element getFirstChildElement(Element parent) {
        return getFirstChildElement((Node) parent);
    }

    /**
     * Gets the first child element of a node.
     * @param parent the node
     * @return the first child element or null if there are no child elements
     */
    private static Element getFirstChildElement(Node parent) {
        NodeList nodeList = parent.getChildNodes();
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node node = nodeList.item(i);
            if (node instanceof Element) {
                return (Element) node;
            }
        }
        return null;
    }
}

Related

  1. getRootElement(Document doc)
  2. getRootElement(Document doc)
  3. getRootElement(Document doc)
  4. getRootElement(Document document)
  5. getRootElement(Document document)
  6. getRootElement(final Element descendant)
  7. getRootElement(final File definition)
  8. getRootElement(final String respuestaPost)
  9. getRootElement(final XMLEventReader bufferedXmlEventReader)