Java XML Document Create getDocument(File file)

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

Description

Create and return a new xml DOM object parsed from the argument File

License

LGPL

Parameter

Parameter Description
file the document <code>File</code>

Return

a new xml DOM object parsed from the argument File, or null if one cannot be created.

Declaration

public static Document getDocument(File file) 

Method Source Code

//package com.java2s;
/*****************************************************************************
 *                        Web3d Consortium Copyright (c) 2007 - 2008
 *                               Java Source
 *
 * This source is licensed under the GNU LGPL v2.1
 * Please read http://www.gnu.org/copyleft/lgpl.html for more information
 *
 * This software comes with the standard NO WARRANTY disclaimer for any
 * purpose. Use it at your own risk. If there's a problem you get to fix it.
 *
 *****************************************************************************/

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main {
    /**//from   ww w .j a va  2  s.c  o  m
     * Create and return a new xml DOM object parsed from the argument <code>URL</code>
     *
     * @param url the document <code>URL</code>
     * @return a new xml DOM object parsed from the argument <code>URL</code>, or 
     * <code>null</code> if one cannot be created.
     */
    public static Document getDocument(URL url) {
        Document document = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            document = builder.parse(url.openStream());
        }
        // unable to get a document builder factory
        catch (FactoryConfigurationError fce) {
            fce.printStackTrace();
        }
        // Parser with specified options can't be built
        catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        }
        // Errors generated during parsing
        catch (SAXException sxe) {
            sxe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        return (document);
    }

    /**
     * Create and return a new xml DOM object parsed from the argument <code>File</code>
     *
     * @param file the document <code>File</code>
     * @return a new xml DOM object parsed from the argument <code>File</code>, or 
     * <code>null</code> if one cannot be created.
     */
    public static Document getDocument(File file) {
        Document document = null;
        try {
            URL url = file.toURI().toURL();
            document = getDocument(url);
        } catch (MalformedURLException murle) {
            System.out.println(murle.getMessage());
        }
        return (document);
    }
}

Related

  1. getDocument(DOMSource source)
  2. getDocument(File f)
  3. getDocument(File file)
  4. getDocument(File file)
  5. getDocument(File file)
  6. getDocument(File file)
  7. getDocument(final File file)
  8. getDocument(InputStream in)
  9. getDocument(InputStream input)