Java tutorial
//package com.java2s; import java.io.*; import javax.xml.parsers.*; import org.w3c.dom.*; import org.xml.sax.SAXException; public class Main { /** * Loads an XML document from the given file. * @param file XML file to load. * @param isValidating true: XML doc is validated against the DTD specifed * within the XML document, false: otherwise. * This does not work for XML Schemata! See {@link #load(File, Schema)} * for that. * @param isNamespaceAware true: XML doc is namespace aware, false: otherwise. * @return Returns an XML document. * @throws IOException Throws an exception if the file couldn't be read. */ static public Document load(File file, boolean isValidating, boolean isNamespaceAware) throws IOException { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(isValidating); factory.setNamespaceAware(isNamespaceAware); DocumentBuilder builder = factory.newDocumentBuilder(); return (builder.parse(file)); } catch (ParserConfigurationException pce) { throw new IOException("Parser configuration error: " + pce); } catch (SAXException se) { throw new IOException("XML parsing error: " + se); } } }