Java XML Element Root getRootElement(final File definition)

Here you can find the source of getRootElement(final File definition)

Description

Return the root element of the supplied file.

License

Apache License

Parameter

Parameter Description
definition the file to load

Return

the root element

Declaration

public static Element getRootElement(final File definition) throws Exception 

Method Source Code


//package com.java2s;
/*/*from w w  w  .j  av  a2  s .  co m*/
 * Copyright 2004-2005 Stephen McConnell
 *
 * Licensed  under the  Apache License,  Version 2.0  (the "License");
 * you may not use  this file  except in  compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed  under the  License is distributed on an "AS IS" BASIS,
 * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
 * implied.
 *
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class Main {
    /**
     * Return the root element of the supplied file.
     * @param definition the file to load
     * @return the root element
     * @exception Exception if the error occurs during root element establishment
     */
    public static Element getRootElement(final File definition) throws Exception {
        if (!definition.exists()) {
            throw new FileNotFoundException(definition.toString());
        }

        if (!definition.isFile()) {
            final String error = "Source is not a file: " + definition;
            throw new IllegalArgumentException(error);
        }

        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setNamespaceAware(false);
        final Document document = factory.newDocumentBuilder().parse(definition);
        return document.getDocumentElement();
    }

    /**
     * Return the root element of the supplied input stream.
     * @param input the input stream containing a XML definition
     * @return the root element
     * @exception Exception if an error occurs
     */
    public static Element getRootElement(final InputStream input) throws Exception {
        final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setNamespaceAware(false);
        factory.setExpandEntityReferences(false);
        DocumentBuilder builder = factory.newDocumentBuilder();
        return getRootElement(builder, input);
    }

    /**
     * Return the root element of the supplied input stream.
     * @param builder the document builder
     * @param input the input stream containing a XML definition
     * @return the root element
     * @exception Exception if an error occurs
     */
    public static Element getRootElement(final DocumentBuilder builder, final InputStream input) throws Exception {
        final Document document = builder.parse(input);
        return document.getDocumentElement();
    }
}

Related

  1. getRootElement(Document doc)
  2. getRootElement(Document document)
  3. getRootElement(Document document)
  4. getRootElement(Document parent)
  5. getRootElement(final Element descendant)
  6. getRootElement(final String respuestaPost)
  7. getRootElement(final XMLEventReader bufferedXmlEventReader)
  8. getRootElement(String elementName, String namespace, String prefix)
  9. getRootElement(String location)