Java tutorial
//package com.java2s; import java.io.InputStream; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.xml.sax.InputSource; public class Main { /** * 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 It there is an error creating the dom. */ public static Document parse(InputStream is) throws IOException { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); return builder.parse(is); } catch (Exception e) { IOException thrown = new IOException(e.getMessage()); throw thrown; } } /** * 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. */ public static Document parse(InputSource is) throws IOException { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); return builder.parse(is); } catch (Exception e) { IOException thrown = new IOException(e.getMessage()); 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 IOException It there is an error creating the dom. */ public static Document parse(String fileName) throws IOException { try { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = builderFactory.newDocumentBuilder(); return builder.parse(fileName); } catch (Exception e) { System.out.println(e.getMessage()); IOException thrown = new IOException(e.getMessage()); throw thrown; } } }