Java XML Document Create createDocumentFromFile(File file)

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

Description

create Document From File

License

Open Source License

Declaration

public static Document createDocumentFromFile(File file) throws Exception 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

public class Main {
    public static Document createDocumentFromFile(File file) throws Exception {
        DocumentBuilderFactory factory = getDocumentBuilderFactory();
        DocumentBuilder builder;//from ww w  .  j av a  2  s. c o  m
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new Exception(e);
        }
        return builder.parse(file);
    }

    private static DocumentBuilderFactory getDocumentBuilderFactory() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        factory.setXIncludeAware(true);
        return factory;
    }
}

Related

  1. createDocumentBuilder()
  2. createDocumentBuilder(boolean namespaces, boolean validating)
  3. createDocumentBuilderFactory()
  4. createDocumentBuilderFactory(ClassLoader classLoader)
  5. createDocumentFromElement(final Element element)
  6. createDocumentFromResult( final StreamResult result)
  7. createDocumentFromString(String str)
  8. createDocumentFromString(String xmlString)
  9. createDocumentFromXml(String input)