Java XML Document Create createDocument(File file)

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

Description

create org.w3c.dom.Document from file

License

Apache License

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static Document createDocument(File file) throws Exception 

Method Source Code

//package com.java2s;
/*//from w  w  w.  j  a v  a  2s.  c  o  m
 * Copyright 2002-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import java.io.InputStream;

import java.io.Reader;

import java.net.URL;

import javax.xml.parsers.DocumentBuilder;

import org.w3c.dom.Document;

import org.xml.sax.InputSource;

public class Main {
    private static DocumentBuilder builder = null;
    private static final Object lock = new Object();

    /**
     * create org.w3c.dom.Document from file
     * @param file
     * @return
     * @throws Exception
     */
    public static Document createDocument(File file) throws Exception {
        // handler.clear();
        synchronized (lock) {
            return builder.parse(file);
        }
    }

    /**
     * create org.w3c.dom.Document from reader
     * @param reader
     * @return
     * @throws Exception
     */
    public static Document createDocument(Reader reader) throws Exception {
        // handler.clear();
        synchronized (lock) {
            return builder.parse(new InputSource(reader));
        }
    }

    /**
     * create org.w3c.dom.Document from input stream
     * @param inputStream
     * @return
     * @throws Exception
     */
    public static Document createDocument(InputStream inputStream) throws Exception {
        // handler.clear();
        synchronized (lock) {
            return builder.parse(inputStream);
        }
    }

    /**
     * create org.w3c.dom.Document from uri
     * @param uri
     * @return
     * @throws Exception
     */
    public static Document createDocument(String uri) throws Exception {
        // handler.clear();
        URL url = new URL(uri);
        synchronized (lock) {
            return builder.parse(url.openStream());
        }
    }

    /**
     * create org.w3c.dom.Document from URL
     * @param url
     * @return
     * @throws Exception
     */
    public static Document createDocument(URL url) throws Exception {
        // handler.clear();
        synchronized (lock) {
            return builder.parse(url.openStream());
        }
    }
}

Related

  1. createDocument()
  2. createDocument(boolean createRoot)
  3. createDocument(boolean isSecureProcessing)
  4. createDocument(boolean validating, boolean namespaceAware)
  5. createDocument(byte[] data)
  6. createDocument(File file)
  7. createDocument(final File file)
  8. createDocument(final String rootName)
  9. createDocument(InputSource is)