Java tutorial
//package com.java2s; 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.InputSource; import org.xml.sax.SAXException; public class Main { /** * Parse an XML document. * * @param in * The input stream. * * @return A Document. * * @throws SAXException * If there is a parsing error. * @throws IOException * If there is an I/O error. * @throws IllegalArgumentException * If there is a parser configuration error. */ public static Document parse(final InputStream in) throws SAXException, IOException { return parse(new InputSource(in)); } /** * Parse an XML document. * * @param is * The input source. * * @return A Document. * * @throws SAXException * If there is a parsing error. * @throws IOException * If there is an I/O error. * @throws IllegalArgumentException * If there is a parser configuration error. */ public static Document parse(final InputSource is) throws SAXException, IOException { try { final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return builder.parse(is); } catch (final ParserConfigurationException ex) { throw new IllegalArgumentException("DOM parser configuration error: " + ex.getMessage()); } } }