Java tutorial
//package com.java2s; import java.io.IOException; import java.io.InputStream; import java.text.ParseException; 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 { /** * This will parse an InputSource and create a DOM document. * * @param is * The stream to get the XML from. * @return The DOM document. * @throws IOException * It there is an error creating the dom. * @throws ParseException */ public static Document parse(InputSource is) throws IOException, ParseException { final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = builderFactory.newDocumentBuilder(); } catch (final ParserConfigurationException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } try { return builder.parse(is); } catch (final IOException e) { final IOException thrown = new IOException(e.getMessage()); throw thrown; } catch (final SAXException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } } /** * This will parse an XML stream and create a DOM document. * * @param is * The stream to get the XML from. * @return The DOM document. * @throws IOException * @throws IOException * It there is an error creating the dom. * @throws ParseException */ public static Document parse(InputStream is) throws IOException, ParseException { final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = builderFactory.newDocumentBuilder(); } catch (final ParserConfigurationException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } try { return builder.parse(is); } catch (final IOException e) { final IOException thrown = new IOException(e.getMessage()); throw thrown; } catch (final SAXException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } } /** * This will parse an XML stream and create a DOM document. * * @param fileName * The file to get the XML from. * @return The DOM document. * @throws ParserConfigurationException * @throws IOException * It there is an error creating the dom. * @throws ParserConfigurationException * @throws IOException * @throws SAXException * @throws SAXException * @throws ParseException */ public static Document parse(String fileName) throws IOException, ParseException { final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = builderFactory.newDocumentBuilder(); } catch (final ParserConfigurationException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } try { return builder.parse(fileName); } catch (final IOException e) { final IOException thrown = new IOException(e.getMessage()); throw thrown; } catch (final SAXException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } } }