Java XML Document from File loadDocument(File documentFile)

Here you can find the source of loadDocument(File documentFile)

Description

Loads the given file using the default XML DOM implementation.

License

Open Source License

Parameter

Parameter Description
documentFile The file to load.

Exception

Parameter Description
ParserConfigurationException an exception
SAXException an exception
IOException an exception

Return

The DOM document contained in the file.

Declaration

public static Document loadDocument(File documentFile)
        throws ParserConfigurationException, SAXException, IOException 

Method Source Code


//package com.java2s;
/*--------------------------------------------------------------------------
 * Copyright (c) 2004, 2006 OpenMethods, LLC
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:/*from  ww  w  .jav a  2  s  .c  om*/
 *    Trip Gilman (OpenMethods), Lonnie G. Pryor (OpenMethods),
 *    Vincent Pruitt (OpenMethods)
 *    
 *    T.D. Barnes (OpenMethods) - initial API and implementation
 -------------------------------------------------------------------------*/

import java.io.File;
import java.io.IOException;

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 {
    /**
     * Loads the given file using the default XML DOM implementation.
     * 
     * @param documentFile The file to load.
     * @return The DOM document contained in the file.
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public static Document loadDocument(File documentFile)
            throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilder documentBuilder = getDocumentBuilder();

        return documentBuilder.parse(documentFile);
    }

    /**
     * Convenience function to get a DOM document builder object.
     * 
     * @return A new <code>DocumentBuilder</code> instance.
     * @throws ParserConfigurationException
     */
    public static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();

        return documentBuilderFactory.newDocumentBuilder();
    }
}

Related

  1. getXmlDocument(File file)
  2. getXmlDocument(String fileName)
  3. isXMLFile(File file)
  4. load(String file)
  5. load(String sFileName)
  6. loadDocument(File file)
  7. loadDocument(String filePath)
  8. loadRootElement(DocumentBuilder documentBuilder, File file)
  9. loadXML(String filename)