List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
public static String serializeElement(Element element) throws TransformerFactoryConfigurationError, TransformerException { String serializedElement = null; if (element != null) { StringWriter output = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(element), new StreamResult(output)); serializedElement = output.toString(); }/*from w ww . ja v a 2 s .c o m*/ return serializedElement; }
From source file:Main.java
public static String node2String(Node node) throws Exception { if (node == null) return null; StringWriter out = null;//from www. j a v a2s .c o m try { Transformer transformer = transformerFactory.newTransformer(); out = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(out)); return out.toString(); } catch (Exception e) { throw e; } finally { if (out != null) out.close(); } }
From source file:Main.java
public static String transformToString(Document doc) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); transformer.transform(source, result); return writer.toString(); }
From source file:Main.java
public static String bean2Xml(Object bean) throws Exception { StringWriter writer = null;/* www . ja v a 2s.c om*/ try { JAXBContext jc = JAXBContext.newInstance(new Class[] { bean.getClass() }); Marshaller m = jc.createMarshaller(); m.setProperty("jaxb.fragment", Boolean.valueOf(true)); writer = new StringWriter(); m.marshal(bean, writer); return prefix + writer.toString(); } catch (Exception e) { e.printStackTrace(); } finally { close(writer); } return null; }
From source file:Main.java
public static String nodeToString(final Node node) { final TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer;/*from ww w . j a v a 2 s .c o m*/ try { transformer = transFactory.newTransformer(); final StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); buffer.append("\n"); return buffer.toString(); } catch (final Exception e) { throw new RuntimeException(e); } }
From source file:utils.FreeMarkerUtils.java
public static String renderString(String templateString, Map<String, ?> model) { try {//w w w . j a va2s .c o m StringWriter result = new StringWriter(); Template t = new Template("name", new StringReader(templateString), new Configuration()); t.process(model, result); return result.toString(); } catch (Exception e) { // throw e; } return ""; }
From source file:Main.java
/** * //w w w . j ava 2 s. c om * @param xml * @param indent * @return pretty formatted xml */ public static String prettyFormat(String xml, int indent) { try { Source xmlInput = new StreamSource(new StringReader(xml)); 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
/** * convert an XML node to an XML statement * @param node current XML node// w ww. j a v a 2 s . c om * @return XML string */ public static String nodeToString(Node node) { //MagicBooleans.trace("nodeToString(node: " + node + ")"); StringWriter sw = new StringWriter(); try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "no"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { System.out.println("nodeToString Transformer Exception"); } String result = sw.toString(); //MagicBooleans.trace("nodeToString() returning: " + result); return result; }
From source file:Main.java
public static String XMLToString(Document doc) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
/** * Transforms a DOM node to a String.//from ww w . ja v a 2 s.c om * * @param node the node * @return the transformed XML representation * @throws TransformerException if a transformation error occurs. */ public static String transform(Node node) throws TransformerException { StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); Source source = new DOMSource(node); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(source, result); return writer.toString(); }