Java tutorial
//package com.java2s; /*- * Copyright 2009 Diamond Light Source Ltd., Science and Technology * Facilities Council * * This file is part of GDA. * * GDA is free software: you can redistribute it and/or modify it under the * terms of the GNU General Public License version 3 as published by the Free * Software Foundation. * * GDA 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 GDA. If not, see <http://www.gnu.org/licenses/>. */ import java.io.CharArrayWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.net.URL; import org.springframework.util.FileCopyUtils; import org.xml.sax.InputSource; public class Main { /** * Reads the XML document and returns it as a {@link String}. * * @param url the URL of the XML document * * @return the XML document, as a {@link String} * * @throws IOException */ public static String readXmlDocument(URL url) throws IOException { Reader in = new InputStreamReader(url.openStream()); CharArrayWriter out = new CharArrayWriter(); FileCopyUtils.copy(in, out); return out.toString(); } /** * Reads an XML document and returns it as a {@link String}. * * @param source the input source for the XML document * * @return the XML document, as a {@link String} * * @throws IOException */ public static String readXmlDocument(InputSource source) throws IOException { CharArrayWriter out = new CharArrayWriter(); FileCopyUtils.copy(source.getCharacterStream(), out); return out.toString(); } }