Java tutorial
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.util.zip.GZIPInputStream; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; public class Main { private static DocumentBuilderFactory mFactory = null; /** * @param xmlFile is the file to read * @return the Document contained in that file */ public static Document readFile(File xmlFile) { return readFile(xmlFile, false); } /** * @param xmlFile is the file to read * @param compress if true, the file is assumed to be compressed with GZIP * @return the Document contained in that file */ public static Document readFile(File xmlFile, boolean compress) { ensureFactory(); try { DocumentBuilder builder = mFactory.newDocumentBuilder(); //builder.setEntityResolver(new EntityUtils()); InputStream is = new FileInputStream(xmlFile); if (compress) is = new GZIPInputStream(is); Document ret = builder.parse(is); is.close(); return ret; } catch (Exception e) { e.printStackTrace(); return null; } } private static synchronized void ensureFactory() { if (mFactory == null) { mFactory = DocumentBuilderFactory.newInstance(); mFactory.setValidating(false); mFactory.setCoalescing(true); mFactory.setExpandEntityReferences(false); } } }