Java XML Document Create getDocument(File f)

Here you can find the source of getDocument(File f)

Description

Parse a DOM Document from the given XML file.

License

Open Source License

Parameter

Parameter Description
f File to parse as Document

Exception

Parameter Description
IOException an exception

Return

Document

Declaration

public static Document getDocument(File f) throws IOException 

Method Source Code


//package com.java2s;
/* XmlUtils/* www.jav  a2  s  . c o m*/
 *
 * Created on Sep 19, 2007
 *
 * Copyright (C) 2007 Internet Archive.
 *
 * This file is part of the Heritrix web crawler (crawler.archive.org).
 *
 * Heritrix is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * any later version.
 *
 * Heritrix 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 Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with Heritrix; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

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 {
    /**
     * Parse a DOM Document from the given XML file. 
     * 
     * @param f File to parse as Document
     * @return Document
     * @throws IOException
     */
    public static Document getDocument(File f) throws IOException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true); // never forget this!
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.parse(f);
        } catch (ParserConfigurationException e) {
            IOException ioe = new IOException();
            ioe.initCause(e);
            throw ioe;
        } catch (SAXException e) {
            IOException ioe = new IOException();
            ioe.initCause(e);
            throw ioe;
        }
    }
}

Related

  1. getDocument()
  2. getDocument()
  3. getDocument()
  4. getDocument(Document document)
  5. getDocument(DOMSource source)
  6. getDocument(File file)
  7. getDocument(File file)
  8. getDocument(File file)
  9. getDocument(File file)