Java XML Document from File readDocument(String fileName)

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

Description

Reads document from file

License

Open Source License

Parameter

Parameter Description
fileName a parameter

Exception

Parameter Description
SAXException an exception
IOException an exception

Declaration

public static Document readDocument(String fileName) throws SAXException, IOException 

Method Source Code


//package com.java2s;
/* ========================================================================
* Copyright (c) 2005-2015 The OPC Foundation, Inc. All rights reserved.
*
* OPC Foundation MIT License 1.00//  w w  w  .  j  a va 2s. c o  m
* 
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
* 
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* The complete license agreement can be found here:
* http://opcfoundation.org/License/MIT/1.00/
* ======================================================================*/

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 {
    /**
     * Reads document from file
     * @param fileName
     * @return
     * @throws SAXException
     * @throws IOException
     */
    public static Document readDocument(String fileName) throws SAXException, IOException {
        DocumentBuilder docBuilder;
        Document result = null;
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        docBuilderFactory.setIgnoringElementContentWhitespace(true);
        try {
            docBuilder = docBuilderFactory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            // unexpected - configuration is assumed to be correct
            throw new Error(e);
        }
        File sourceFile = new File(fileName);
        result = docBuilder.parse(sourceFile);
        return result;
    }
}

Related

  1. read(InputStream input)
  2. read(InputStream xml)
  3. read(java.io.File file)
  4. read(String file)
  5. read(String filename)
  6. readDocumentXML(File file)
  7. readXml(File file)
  8. readXML(File file)
  9. readXML(File file)