Java XML Element First getFirstElement(Document document)

Here you can find the source of getFirstElement(Document document)

Description

get first "real" element (ie not the document-rootelement, but the next one

License

Open Source License

Declaration

public static Element getFirstElement(Document document) 

Method Source Code


//package com.java2s;
/*/*from  ww  w.j  a v  a 2s. co  m*/
 * Helma License Notice
 *
 * The contents of this file are subject to the Helma License
 * Version 2.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://adele.helma.org/download/helma/license.txt
 *
 * Copyright 1998-2003 Helma Software. All Rights Reserved.
 *
 * $RCSfile$
 * $Author$
 * $Revision$
 * $Date$
 */

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

public class Main {
    /**
     * get first "real" element (ie not the document-rootelement, but the next one
     */
    public static Element getFirstElement(Document document) {
        Element workelement = null;

        if (document.getDocumentElement() != null) {
            org.w3c.dom.Node tmp = document.getDocumentElement().getFirstChild();

            while (tmp != null) {
                tmp = tmp.getNextSibling();

                if (tmp.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                    workelement = (Element) tmp;

                    break;
                }
            }
        }

        return workelement;
    }
}

Related

  1. getFirstElement(Document document, String tagName)
  2. getFirstElement(Element documentElement)
  3. getFirstElement(String name, Element root)
  4. getFirstElementByTagName(Document doc, String tagName)