Java tutorial
//package com.java2s; /** * This file is protected by Copyright. Please refer to the COPYRIGHT file * distributed with this source distribution. * * This file is part of REDHAWK. * * REDHAWK 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. * * REDHAWK 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 this program. If not, see http://www.gnu.org/licenses/. */ import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.SAXException; public class Main { /** Reads in an XML document from a local file. * @param file The file to read in. * @return The parsed XML document. * @throws ParserConfigurationException If there is an error in the default * XML parser configuration. * @throws IOException If there is an error reading the file. * @throws SAXException If there is an error in parsing the file. */ public static Document readDocumentXML(File file) throws ParserConfigurationException, IOException, SAXException { // Note that we are using: // new BufferedInputStream(new FileInputStream(...)) // rather than: // new FileInputStream(...) // as the later version can be extremely slow on some systems. DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); InputStream in = new BufferedInputStream(new FileInputStream(file)); return builder.parse(in); } }