Java XML Parse File parseXML(final File file)

Here you can find the source of parseXML(final File file)

Description

Parses a DOM from the given XML file on disk.

License

Open Source License

Declaration

public static Document parseXML(final File file) 

Method Source Code


//package com.java2s;
/*/*  w  ww. j  a v  a  2 s  .  c  o  m*/
 * #%L
 * VisBio application for visualization of multidimensional biological
 * image data.
 * %%
 * Copyright (C) 2002 - 2014 Board of Regents of the University of
 * Wisconsin-Madison.
 * %%
 * 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 2 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/gpl-2.0.html>.
 * #L%
 */

import java.io.ByteArrayInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

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 {
    /** Parses a DOM from the given XML file on disk. */
    public static Document parseXML(final File file) {
        try {
            return parseXML(new FileInputStream(file));
        } catch (final FileNotFoundException exc) {
            exc.printStackTrace();
        }
        return null;
    }

    /** Parses a DOM from the given XML string. */
    public static Document parseXML(final String xml) {
        return parseXML(new ByteArrayInputStream(xml.getBytes()));
    }

    /** Parses a DOM from the given XML input stream. */
    public static Document parseXML(final InputStream is) {
        try {
            final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            final DocumentBuilder db = dbf.newDocumentBuilder();
            return db == null ? null : db.parse(is);
        } catch (final IOException exc) {
            exc.printStackTrace();
        } catch (final ParserConfigurationException exc) {
            exc.printStackTrace();
        } catch (final SAXException exc) {
            exc.printStackTrace();
        }
        return null;
    }
}

Related

  1. parseFile(String fname)
  2. parsePage(String filePath)
  3. parseXml(File file)
  4. parseXml(File file)
  5. parseXml(File file)
  6. parseXml(String fileName)
  7. parseXml(String filename, boolean validating)
  8. parseXml(String filePath)
  9. parseXml(String filePath)