Java tutorial
//package com.java2s; /* * Metadata Editor * @author Jiri Kremser * * * * Metadata Editor - Rich internet application for editing metadata. * Copyright (C) 2011 Jiri Kremser (kremser@mzk.cz) * Moravian Library in Brno * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * 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 for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * */ import java.io.IOException; import java.io.InputStream; import java.io.StringReader; 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 { /** * Parses the document. * * @param is * the is * @return the document * @throws ParserConfigurationException * the parser configuration exception * @throws SAXException * the sAX exception * @throws IOException * Signals that an I/O exception has occurred. */ public static Document parseDocument(InputStream is) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return builder.parse(is); } /** * Parses the document. * * @param xml * the xml * @return the document * @throws ParserConfigurationException * the parser configuration exception * @throws SAXException * the sAX exception * @throws IOException * Signals that an I/O exception has occurred. */ public static Document parseDocument(String xml) throws ParserConfigurationException, SAXException, IOException { DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); return builder.parse(new InputSource(new StringReader(xml))); } public static Document parseDocument(String xml, boolean namespaceaware) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(namespaceaware); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(new InputSource(new StringReader(xml))); } /** * Parses the document. * * @param is * the is * @param namespaceaware * the namespaceaware * @return the document * @throws ParserConfigurationException * the parser configuration exception * @throws SAXException * the sAX exception * @throws IOException * Signals that an I/O exception has occurred. */ public static Document parseDocument(InputStream is, boolean namespaceaware) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(namespaceaware); DocumentBuilder builder = factory.newDocumentBuilder(); return builder.parse(is); } }