Java tutorial
//package com.java2s; /** * This file is part of the au-xml-util package * * Copyright Trenton D. Adams <trenton daught d daught adams at gmail daught ca> * * au-xml-util is free software: you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or (at your * option) any later version. * * au-xml-util 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 Lesser General Public * License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with au-xml-util. If not, see <http://www.gnu.org/licenses/>. * * See the COPYING file for more information. */ import org.w3c.dom.Document; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import java.io.*; public class Main { /** * Example for retrieving the APAS institutions list * * @param xml the string representation of the XML * * @return the Document object created from the XML string representation * * @throws IOException if an I/O error occurs * @throws SAXException if an XML parsing exception occurs. * @throws ParserConfigurationException if a JAXP configuration error * occurs. */ public static Document stringToDocument(final String xml) throws SAXException, IOException, ParserConfigurationException { return loadXMLFrom(new InputSource(new StringReader(xml))); } /** * Loads an XML document from the input stream into a DOM Document. * * @param is the input stream to load from * * @return the new Document * * @throws SAXException if a SAX parsing error occurs * @throws IOException if an IO error occurs * @throws ParserConfigurationException if a JAXP configuration parsing * error occurs */ public static Document loadXMLFrom(final InputStream is) throws SAXException, IOException, ParserConfigurationException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = null; builder = factory.newDocumentBuilder(); assert builder != null; final Document doc = builder.parse(is); is.close(); return doc; } public static Document loadXMLFrom(final InputSource is) throws ParserConfigurationException, IOException, SAXException { final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder builder = null; builder = factory.newDocumentBuilder(); assert builder != null; final Document doc = builder.parse(is); return doc; } }