Java tutorial
//package com.java2s; /*L * Copyright Washington University in St. Louis, SemanticBits, Persistent Systems, Krishagni. * * Distributed under the OSI-approved BSD 3-Clause License. * See http://ncip.github.com/commons-module/LICENSE.txt for details. */ 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.ParserConfigurationException; import org.w3c.dom.Document; import org.xml.sax.SAXException; public class Main { /** * This method returns the Document object for xml file. * @param fileName File name. * @return Document xml document. * @throws ParserConfigurationException throws this exception if DocumentBuilderFactory not created. * @throws IOException throws this exception if file not found. * @throws SAXException throws this exception if not able to parse xml file. */ public static Document getDocument(String fileName) throws ParserConfigurationException, SAXException, IOException { File file = new File(fileName); DocumentBuilder documentBuilder = getDocumentBuilder(); return documentBuilder.parse(file); } /** * This method returns the Document object for input stream. * @param inputStream InputStream of xml file . * @return Document object for input stream. * @throws ParserConfigurationException throws this exception if DocumentBuilderFactory not created. * @throws IOException throws this exception if file not found. * @throws SAXException throws this exception if not able to parse xml file. */ public static Document getDocument(InputStream inputStream) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder documentBuilder = getDocumentBuilder(); return documentBuilder.parse(inputStream); } /** * @return DocumentBuilder object * @throws ParserConfigurationException if a DocumentBuilder * cannot be created which satisfies the configuration requested. */ private static DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); return dbf.newDocumentBuilder(); } }