Java tutorial
//package com.java2s; /* * Keystone Development Framework * Copyright (C) 2004-2009 Rick Knowles * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public License * Version 2 as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License Version 2 for more details. * * You should have received a copy of the GNU Library General Public License * Version 2 along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.io.Reader; 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 { public static Document parseFileToXML(File in) { try { InputStream defXML = new FileInputStream(in); Document defDoc = parseStreamToXML(defXML); defXML.close(); return defDoc; } catch (IOException errIO) { throw new RuntimeException("Error parsing XML files", errIO); } } public static Document parseStreamToXML(InputStream in) { try { // Use JAXP to create a document builder DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setExpandEntityReferences(false); builderFactory.setValidating(false); builderFactory.setNamespaceAware(true); builderFactory.setIgnoringComments(true); builderFactory.setCoalescing(true); builderFactory.setIgnoringElementContentWhitespace(true); return builderFactory.newDocumentBuilder().parse(in); } catch (ParserConfigurationException errParser) { throw new RuntimeException("Error getting XML parser", errParser); } catch (SAXException errSax) { throw new RuntimeException("Error parsing XML files", errSax); } catch (IOException errIO) { throw new RuntimeException("Error parsing XML files", errIO); } } public static Document parseStreamToXML(Reader in) { try { // Use JAXP to create a document builder DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setExpandEntityReferences(false); builderFactory.setValidating(false); builderFactory.setNamespaceAware(true); builderFactory.setIgnoringComments(true); builderFactory.setCoalescing(true); builderFactory.setIgnoringElementContentWhitespace(true); return builderFactory.newDocumentBuilder().parse(new InputSource(in)); } catch (ParserConfigurationException errParser) { throw new RuntimeException("Error getting XML parser", errParser); } catch (SAXException errSax) { throw new RuntimeException("Error parsing XML files", errSax); } catch (IOException errIO) { throw new RuntimeException("Error parsing XML files", errIO); } } }