Java XML Parse File parse(File file)

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

Description

parse

License

Apache License

Declaration

public static Document parse(File file) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Document;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;
import java.net.URI;

public class Main {
    public static Document parse(File file) throws IOException {
        try {/*w ww.j  a  v  a 2  s.  c  o  m*/
            return createDocumentBuilder().parse(file);
        } catch (Exception e) {
            throw new IOException(e);
        }
    }

    public static Document parse(InputStream input) throws IOException {
        try {
            return createDocumentBuilder().parse(input);
        } catch (Exception e) {
            throw new IOException(e);
        }
    }

    public static DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builderFactory.setNamespaceAware(true);
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        builder.setEntityResolver(new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
                InputStream source = !systemId.startsWith("file:") ? null
                        : getClass().getResourceAsStream(
                                "/net/sf/logsupport/" + new File(URI.create(systemId)).getName());

                return source == null ? new InputSource(new StringReader("")) : new InputSource(source);
            }
        });
        return builder;
    }
}

Related

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