List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
private static String transformToStringResult(Document document, Transformer transformer) throws TransformerConfigurationException, TransformerException { StringWriter stringWriter = new StringWriter(); synchronized (transformer) { transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); }// ww w . j ava2 s. c om return stringWriter.toString(); }
From source file:Main.java
public static String getXml(Document doc) { try {/*from w ww. j a va 2 s . c o m*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); String xmlString = result.getWriter().toString(); return xmlString; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:io.uengine.util.StringUtils.java
/** * Properties? key value ? ./* w ww. j a va 2 s. c o m*/ * * @param properties Properties * @return key value ? */ public static String propertiesToString(Properties properties) { StringWriter writer = new StringWriter(); PrintWriter out = new PrintWriter(writer); Set<Object> keys = properties.keySet(); for (Object key : keys) { out.println(key + "=" + properties.get(key)); } return writer.getBuffer().toString(); }
From source file:Main.java
/** * convert org.w3c.dom.Document to xml-String * /*from w w w. j a va 2s . co m*/ * @param document * @return String xml * @throws Exception */ private static String toString(Document document) throws Exception { Source source = new DOMSource(document); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); }
From source file:edu.unc.lib.dl.util.SOAPUtil.java
public static String getString(SOAPMessage msg) { StringWriter w = new StringWriter(); StreamResult result = new StreamResult(w); print(msg, result);//from www. jav a2 s . com w.flush(); return w.toString(); }
From source file:Main.java
public static String createString(Element element) { try {//from w w w. j av a2 s . c om TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); DOMSource source = new DOMSource(element); Writer writer = new StringWriter(); Result result = new StreamResult(writer); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); return writer.toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return ""; }
From source file:org.dthume.maven.xpom.impl.XPOMUtil.java
public static Source modelToSource(final Model model) throws IOException { final StringWriter writer = new StringWriter(); new MavenXpp3Writer().write(writer, model); return new StringSource(writer.toString()); }
From source file:Main.java
public static String getXMLAsString(Element eElement) throws Exception { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(eElement); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:Main.java
public static String getStringFromDocument(Document doc) { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer;// w w w .j a v a 2 s . c om try { transformer = tf.newTransformer(); transformer.transform(domSource, result); } catch (Exception e) { throw new IllegalStateException(e); } return writer.toString(); }
From source file:Main.java
public static String jaxbToString(Class<?> xmlClass, JAXBElement<?> jaxbElement) { // Make sure we are given the correct input. if (xmlClass == null || jaxbElement == null) { return null; }/* w w w . j ava 2 s. c om*/ // We will write the XML encoding into a string. StringWriter writer = new StringWriter(); String result; try { // We will use JAXB to marshal the java objects. final JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass); // Marshal the object. Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(jaxbElement, writer); result = writer.toString(); } catch (Exception e) { // Something went wrong so get out of here. return null; } finally { try { writer.close(); } catch (IOException ex) { } } // Return the XML string. return result; }