Java XML Document Create getDocument(String fileName)

Here you can find the source of getDocument(String fileName)

Description

This method returns the Document object for xml file.

License

BSD License

Parameter

Parameter Description
fileName File name.

Exception

Parameter Description
ParserConfigurationException throws this exception if DocumentBuilderFactory not created.
IOException throws this exception if file not found.
SAXException throws this exception if not able to parse xml file.

Return

Document xml document.

Declaration

public static Document getDocument(String fileName)
        throws ParserConfigurationException, SAXException, IOException 

Method Source Code


//package com.java2s;
/*L/*ww w.  ja va  2s.c o m*/
 * Copyright Washington University in St. Louis, SemanticBits, Persistent Systems, Krishagni.
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/commons-module/LICENSE.txt for details.
 */

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
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 {
    /**
     * This method returns the Document object for xml file.
     * @param fileName File name.
     * @return Document xml document.
     * @throws ParserConfigurationException throws this exception if DocumentBuilderFactory not created.
     * @throws IOException throws this exception if file not found.
     * @throws SAXException throws this exception if not able to parse xml file.
     */
    public static Document getDocument(String fileName)
            throws ParserConfigurationException, SAXException, IOException {
        File file = new File(fileName);
        DocumentBuilder documentBuilder = getDocumentBuilder();
        return documentBuilder.parse(file);
    }

    /**
     * This method returns the Document object for input stream.
     * @param inputStream InputStream of xml file .
     * @return Document object for input stream.
     * @throws ParserConfigurationException throws this exception if DocumentBuilderFactory not created.
     * @throws IOException throws this exception if file not found.
     * @throws SAXException throws this exception if not able to parse xml file.
     */
    public static Document getDocument(InputStream inputStream)
            throws ParserConfigurationException, SAXException, IOException {

        DocumentBuilder documentBuilder = getDocumentBuilder();
        return documentBuilder.parse(inputStream);
    }

    /**
     * @return DocumentBuilder object
     * @throws ParserConfigurationException if a DocumentBuilder
      * cannot be created which satisfies the configuration requested.
     */
    private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        return dbf.newDocumentBuilder();
    }
}

Related

  1. getDocument(InputStream xmlInputStream)
  2. getDocument(Node node)
  3. getDocument(Node parent)
  4. getDocument(Resource resource)
  5. getDocument(String docPath)
  6. getDocument(String filePath)
  7. getDocument(String payload)
  8. getDocument(String xml)
  9. getDocument(String xml)