Java tutorial
//package com.java2s; import org.w3c.dom.Document; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.*; public class Main { /** * Returns a {@link org.w3c.dom.Document} from an XML file. * @param file the XML file * @return the XML document * @throws IOException if there was an error with the file * @throws SAXException if there was an error while parsing the XML document * @throws javax.xml.parsers.ParserConfigurationException */ public static Document getDocument(File file) throws IOException, SAXException, ParserConfigurationException { return getDocument(new FileInputStream(file)); } /** * Returns a {@link org.w3c.dom.Document} from an {@link java.io.InputStream}. * @param inputStream the stream from which getting the XML content * @return the XML document or null if * @throws IOException if there was an error while getting the XML from the stream * @throws SAXException if there was an error while parsing the XML document * @throws javax.xml.parsers.ParserConfigurationException */ public static Document getDocument(InputStream inputStream) throws IOException, SAXException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(inputStream); } }