List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
/** * Node to string.// w w w. j a v a2 s . com * * @param node the node * @return the string */ public static String nodeToString(final Node node) { final StringWriter sw = new StringWriter(); try { final Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (final TransformerException te) { System.out.println("nodeToString Transformer Exception"); } return sw.toString(); }
From source file:Main.java
/** * Transforms the given XML document into its textual representation. * * @param doc the document to transform. * @return the XML document transformed to a string. * * @throws TransformerException if the transformation failed. *//*from w w w . j a va 2s . c om*/ public static String documentToString(Document doc) throws TransformerException { StreamResult result = new StreamResult(new StringWriter()); transform(doc, result); String xmlString = result.getWriter().toString(); return xmlString; }
From source file:Main.java
public static String prettyFormat(String input, int indent) { try {/*from ww w.j a v a2 s . c om*/ Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:Main.java
/** * Marshals a bean to XML.//from w ww .ja v a 2 s .c o m * * @param bean * @param namespaceURI * @param localPart * @return XML {@link String} * @throws JAXBException */ @SuppressWarnings("unchecked") public static <T> String marshal(T bean, String namespaceURI, String localPart) throws JAXBException { QName qName = new QName(namespaceURI, localPart); Class<T> clazz = (Class<T>) bean.getClass(); Marshaller marshaller = createMarshaller(clazz); Writer stw = new StringWriter(); marshaller.marshal(new JAXBElement<T>(qName, clazz, bean), stw); return stw.toString(); }
From source file:Main.java
public static String getStringFromDocument(Document doc) throws TransformerException { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); }
From source file:com.joliciel.csvLearner.utils.LogUtils.java
/** Return the current exception & stack trace as a String. *//* w ww . ja v a 2s . c o m*/ public static String getErrorString(Throwable e) { String s = null; try { StringWriter sw = new StringWriter(); PrintWriter ps = new PrintWriter((Writer) sw); e.printStackTrace(ps); sw.flush(); s = sw.toString(); sw.close(); } catch (IOException ioe) { // do nothing! } return s; }
From source file:Util.java
/** * Applies a stylesheet to a given xml document. * /*from ww w.j av a 2 s . c om*/ * @param xmlDocument * the xml document to be transformed * @param xsltFilename * the filename of the stylesheet * @return the transformed xml document * @throws Exception */ public static String transformDocumentAsString(Document xmlDocument, String xsltFilename) throws Exception { // Generate a Transformer. Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(xsltFilename)); StringWriter stringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(stringWriter); // Perform the transformation. transformer.transform(new DOMSource(xmlDocument), streamResult); // Now you can get the output Node from the DOMResult. return stringWriter.toString(); }
From source file:Main.java
public static String saveInString(Document doc) { StreamResult streamResult = new StreamResult(new StringWriter()); save(doc, streamResult);/* w w w .j a v a2 s . co m*/ return streamResult.getWriter().toString(); }
From source file:Main.java
public static String getStringFromNode(Node node) throws Exception { StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(writer)); String xml = writer.toString(); return xml;/* w ww. j a v a 2 s.c o m*/ }
From source file:Main.java
/** * Converts XML document into the string. * @param document XML/* ww w .j av a 2 s .c om*/ * @return XML as string * @throws TransformerException */ public static String toString(Document document) throws TransformerException { DOMSource domSource = new DOMSource(document); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); }