Java XML Document from File load(String sFileName)

Here you can find the source of load(String sFileName)

Description

load

License

LGPL

Declaration

public static Document load(String sFileName) 

Method Source Code

//package com.java2s;
/**/*ww w.j  a v  a  2s. c om*/
 *  Copyright (C) 2008-2014  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.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.gnu.org/licenses/lgpl.html
 *
 *  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.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 {
    public static Document load(String sFileName) {
        if (sFileName != null) {
            return load(new File(sFileName));
        } else {
            throw new RuntimeException("XML error : load(null) ");
        }
    }

    public static Document load(File file) {
        if (file != null) {
            DocumentBuilder builder = getDocumentBuilder();
            Document doc = null;
            try {
                doc = builder.parse(file);
            } catch (SAXException e) {
                throw new RuntimeException("XML error : Cannot parse : SAXException", e);
            } catch (IOException e) {
                throw new RuntimeException("XML error : Cannot parse : IOException", e);
            }
            return doc;
        } else {
            throw new IllegalArgumentException("XML error : load(null) ");
        }
    }

    public static Document load(InputStream is) {
        if (is != null) {
            DocumentBuilder builder = getDocumentBuilder();
            Document doc = null;
            try {
                doc = builder.parse(is);
            } catch (SAXException e) {
                throw new RuntimeException("XML error : Cannot parse : SAXException", e);
            } catch (IOException e) {
                throw new RuntimeException("XML error : Cannot parse : IOException", e);
            }
            return doc;
        } else {
            throw new IllegalArgumentException("XML error : load(null) ");
        }
    }

    private static DocumentBuilder getDocumentBuilder() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
        } catch (ParserConfigurationException e) {
            throw new RuntimeException("XML error : Cannot get DocumentBuilder", e);
        }
        return builder;
    }
}

Related

  1. getXmlDocument(File file)
  2. getXmlDocument(File file)
  3. getXmlDocument(String fileName)
  4. isXMLFile(File file)
  5. load(String file)
  6. loadDocument(File documentFile)
  7. loadDocument(File file)
  8. loadDocument(String filePath)
  9. loadRootElement(DocumentBuilder documentBuilder, File file)