Java XML Parse File parse(File f, boolean validation)

Here you can find the source of parse(File f, boolean validation)

Description

parse

License

Open Source License

Declaration

public static Document parse(File f, boolean validation) 

Method Source Code

//package com.java2s;
/*   FILE: Utils.java//from  w w  w .j  ava2  s  .  c  o  m
 *   DATE OF CREATION:   Thu Jan 09 14:14:35 2003
 *   Copyright (c) Emmanuel Pietriga, 2002-2011. All Rights Reserved
 *   Licensed under the GNU LGPL. For full terms see the file COPYING.
 *
 *$Id: AppletUtils.java 4936 2013-02-21 13:55:55Z epietrig $
 */

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.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class Main {
    public static Document parse(File f, boolean validation) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(validation);
            if (!validation) {
                factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",
                        new Boolean(false));
            }
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document res = builder.parse(f);
            return res;
        } catch (FactoryConfigurationError e) {
            e.printStackTrace();
            return null;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            return null;
        } catch (SAXException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Document parse(InputStream is, boolean validation) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(validation);
            if (!validation) {
                factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",
                        new Boolean(false));
            }
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document res = builder.parse(is);
            return res;
        } catch (FactoryConfigurationError e) {
            e.printStackTrace();
            return null;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            return null;
        } catch (SAXException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static Document parse(String uri, boolean validation) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(validation);
            if (!validation) {
                factory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd",
                        new Boolean(false));
            }
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document res = builder.parse(uri);
            return res;
        } catch (FactoryConfigurationError e) {
            e.printStackTrace();
            return null;
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
            return null;
        } catch (SAXException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. parse(File f)
  2. parse(File f)
  3. parse(File f)
  4. parse(File f, DefaultHandler dh)
  5. parse(File file)
  6. parse(File file)
  7. parse(File file)