Java XML Document from File readDocumentXML(File file)

Here you can find the source of readDocumentXML(File file)

Description

Reads in an XML document from a local file.

License

Open Source License

Parameter

Parameter Description
file The file to read in.

Exception

Parameter Description
ParserConfigurationException If there is an error in the defaultXML parser configuration.
IOExceptionIf there is an error reading the file.
SAXException If there is an error in parsing the file.

Return

The parsed XML document.

Declaration

public static Document readDocumentXML(File file)
        throws ParserConfigurationException, IOException, SAXException 

Method Source Code

//package com.java2s;
/**/*from  w  ww  . jav  a 2s .c o m*/
 * 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);
    }
}

Related

  1. read(InputStream xml)
  2. read(java.io.File file)
  3. read(String file)
  4. read(String filename)
  5. readDocument(String fileName)
  6. readXml(File file)
  7. readXML(File file)
  8. readXML(File file)
  9. readXML(File file)