List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
private static String toString(final Document document, final boolean indent, final boolean omitXmlDeclaration) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); final StringWriter stringWriter = new StringWriter(); final StreamResult streamResult = new StreamResult(stringWriter); transformer.transform(new DOMSource(document), streamResult); return stringWriter.toString(); }
From source file:Main.java
private static String indent(String xml) { try {/*from ww w.ja v a2 s .com*/ InputSource is = new InputSource(new StringReader(xml)); Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is); OutputFormat format = new OutputFormat(); format.setIndenting(true); format.setIndent(4); Writer out = new StringWriter(); new XMLSerializer(out, format).serialize(document); return out.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String toString(Document doc, String encoding, boolean indent) throws TransformerFactoryConfigurationError, TransformerException { Source source = new DOMSource(doc); StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); if (indent) { xformer.setOutputProperty(OutputKeys.INDENT, "yes"); }/*from w ww . java 2s. c o m*/ xformer.transform(source, result); return sw.getBuffer().toString(); }
From source file:Main.java
/** * print document to string//from ww w . j ava 2s.c o m * @param doc * @return * @throws TransformerException */ public static String toString(Document doc) throws TransformerException { Transformer tf = tff.newTransformer(); StringWriter writer = new StringWriter(); tf.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; }
From source file:Main.java
public static String prettyPrint(Document doc) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer;//w w w .ja v a 2 s. c om StringWriter writer = new StringWriter(); try { serializer = tfactory.newTransformer(); //Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$ serializer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static String xmlToString(Document doc) { try {//from ww w .j a v a2 s. c o m Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerException ex) { System.err.println("Error: Cannot load xml transformer"); System.err.println("Cause: " + ex.toString()); System.exit(1); return null; } }
From source file:net.fizzl.redditengine.data.MessageListing.java
public static MessageListing fromInputStream(InputStream is) throws IOException { StringWriter writer = new StringWriter(); IOUtils.copy(is, writer, "UTF-8"); return fromString(writer.toString()); }
From source file:Main.java
public static String docToString(Document doc, boolean formated) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {/* ww w . ja v a 2s . c o m*/ transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); return null; } transformer.setOutputProperty(OutputKeys.METHOD, "xml"); if (formated) { // linefeed formatting transformer.setOutputProperty(OutputKeys.INDENT, "yes"); } else { // remove xml header transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); DOMSource domSource = new DOMSource(doc); try { transformer.transform(domSource, sr); } catch (TransformerException e) { e.printStackTrace(); return null; } return sw.toString(); }
From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.sampling.XStreamTools.java
public static String toXML(Object object) throws IOException { Writer out = new StringWriter(); out.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); XStream xStream = getXStream();//from w ww .j a va 2s.c o m xStream.toXML(object, out); IOUtils.closeQuietly(out); return out.toString(); }
From source file:com.mirth.connect.connectors.http.HttpUtil.java
public static String uncompressGzip(byte[] content, String charset) throws IOException { GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(content)); StringWriter writer = new StringWriter(); IOUtils.copy(gzis, writer, charset); return writer.toString(); }