Java XML Document from File readXML(File file)

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

Description

Reads XML from a file

License

Open Source License

Parameter

Parameter Description
file the URL to fetch

Exception

Parameter Description
IOException , SAXException if an error occurs when reading from the stream

Return

The document

Declaration

public static Document readXML(File file) throws IOException, SAXException 

Method Source Code


//package com.java2s;
/*/*from  www  .j a v  a  2 s  .c o m*/
 * FFNLauncher
 * Copyright (C) 2013 Abel Hoogeveen <http://www.sigmacoders.nl>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import java.net.UnknownHostException;

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

import org.w3c.dom.Document;

import org.xml.sax.SAXException;

public class Main {
    /**
     * Reads XML from a file
     * @param file the URL to fetch
     * @return The document
     * @throws IOException, SAXException if an error occurs when reading from the stream
     */
    public static Document readXML(File file) throws IOException, SAXException {
        return getXML(new FileInputStream(file));
    }

    /**
     * Reads XML from a stream
     * @param stream the stream to read the document from
     * @return The document
     * @return The document
     * @throws IOException, SAXException if an error occurs when reading from the stream
     */
    public static Document getXML(InputStream stream) throws IOException, SAXException {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        try {
            return docFactory.newDocumentBuilder().parse(stream);
        } catch (ParserConfigurationException ignored) {
            System.out.println("getXML parser error v001");
        } catch (UnknownHostException e) {
            System.out.println("getXML UnknowHost error v002");
        }
        return null;
    }
}

Related

  1. read(String filename)
  2. readDocument(String fileName)
  3. readDocumentXML(File file)
  4. readXml(File file)
  5. readXML(File file)
  6. readXML(File file)
  7. readXML(File xml)
  8. readXml(String path)
  9. readXML(String path)