Here you can find the source of getRootElement(final File definition)
Parameter | Description |
---|---|
definition | the file to load |
public static Element getRootElement(final File definition) throws Exception
//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(); } }