Java XML Parse File parseFile(final File aFile)

Here you can find the source of parseFile(final File aFile)

Description

parse File

License

Open Source License

Declaration

public static Document parseFile(final File aFile) throws SAXException, ParserConfigurationException 

Method Source Code


//package com.java2s;
// This file is part of nzdis-util package. See the file LICENSE for copyright information

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

import java.io.Reader;
import java.io.StringReader;
import java.util.Properties;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class Main {
    public static Document parseFile(final File aFile) throws SAXException, ParserConfigurationException {
        Properties props;/*  w w  w . ja v a 2s . co  m*/
        props = new Properties();
        return parseFile(aFile, props);
    }

    public static Document parseFile(final File aFile, final String propfilename)
            throws SAXException, ParserConfigurationException {
        Properties props;
        props = new Properties();
        try {
            InputStream propsStream = new FileInputStream(propfilename);
            props.load(propsStream);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex1) {
            ex1.printStackTrace();
        }
        return parseFile(aFile, props);
    }

    public static Document parseFile(final File aFile, final Properties props)
            throws SAXException, ParserConfigurationException {
        InputStream input = null;
        try {
            input = new FileInputStream(aFile);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
            return null;
        }
        return parse(input, props);
    }

    public static Document parse(InputStream input, Properties props)
            throws SAXException, ParserConfigurationException {
        return parse(new InputSource(input), props);
    }

    public static Document parse(String input) throws SAXException, ParserConfigurationException {
        return parse(new StringReader(input), null);
    }

    public static Document parse(Reader input) throws SAXException, ParserConfigurationException {
        return parse(new InputSource(input), null);
    }

    public static Document parse(Reader input, Properties props) throws SAXException, ParserConfigurationException {
        return parse(new InputSource(input), props);
    }

    public static Document parse(InputSource input, @SuppressWarnings("unused") Properties props)
            throws SAXException, ParserConfigurationException {

        Document doc = null;
        try {
            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            doc = docBuilder.parse(input);

        } catch (IOException e) {
            e.printStackTrace(System.out);
            //throw e;
        }

        return doc;
    }
}

Related

  1. parseData(String filename)
  2. parseDoc(File xmlFile)
  3. parseFile(Class context, String name)
  4. parseFile(File file)
  5. parseFile(File fn)
  6. parseFile(String fileName)
  7. parseFile(String fname)
  8. parsePage(String filePath)
  9. parseXml(File file)