List of usage examples for java.io StringWriter StringWriter
public StringWriter()
From source file:Main.java
/** * Method to transform an XML document into a pretty-formatted string. * @param xml/*w ww . ja v a2 s . c o m*/ * @return * @throws Exception */ public static final String xmlToString(Document xml) throws Exception { Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(xml), new StreamResult(out)); return out.toString(); }
From source file:Main.java
public static String document2String(Document document, String encode) throws TransformerException { String xml = null;/*from w ww . ja va 2s . c o m*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty("encoding", encode); transformer.setOutputProperty("indent", "yes"); StringWriter sw = new StringWriter(); transformer.transform(source, new StreamResult(sw)); xml = sw.toString(); return xml; }
From source file:Main.java
/** * @see //http://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java *///from w w w. j av a 2 s . co m public static String toXML(Document document, boolean format) throws TransformerException { if (format) { removeWhitespaceNodes(document.getDocumentElement()); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); }
From source file:Main.java
/** * Creates XML from W3C Document from the xml. * * @param document the xml that needs to be converted. * @return the XML./*from www .java 2s . c o m*/ * @throws Exception is there is some error during operation. */ public static String createXml(Document document) throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); StringWriter stringWriter = new StringWriter(); StreamResult result = new StreamResult(stringWriter); DOMSource source = new DOMSource(document); transformer.transform(source, result); return stringWriter.toString(); }
From source file:Main.java
public static String createPrintableString(Document doc) { String s = ""; try {/*from www . ja va 2 s . co m*/ // set up a transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); s = sw.toString(); } catch (Exception e) { e.printStackTrace(); } return s; }
From source file:Main.java
private static String getStringFromDocument(Node doc) { try {// w w w. jav a2s . com 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(); } catch (TransformerException ex) { ex.printStackTrace(); return null; } }
From source file:Main.java
public static String toXML(Document document, boolean format) throws Exception { if (format) { removeWhitespaceNodes(document.getDocumentElement()); }// w w w.j a v a 2 s.c om TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 2); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(out)); return out.toString(); }
From source file:Main.java
public static String transform(String xml, String stylesheet) throws TransformerException { StringWriter writer = new StringWriter(); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(new StreamSource(stylesheet)); transformer.transform(new StreamSource(xml), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
public static final String createStringFromDOMNode(Node node, boolean omitDeclaration) { assert node != null; StringWriter out = null;/*w w w . j av a 2s . co m*/ try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); node.normalize(); Source source = new DOMSource(node); out = new StringWriter(); Result resultStream = new StreamResult(out); if (omitDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } else { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); } transformer.transform(source, resultStream); } catch (Exception e) { } if (out != null) { return out.toString(); } return null; }
From source file:Main.java
/** * a debug method/* w ww . jav a 2 s. co m*/ * * @param node A node to be dumped to a string * @param omitDeclaration A boolean whether to omit the XML declaration * @return A string representation of the node. * @throws Exception If anything goes wrong. Error handling omitted. */ public static String dumpNode(Node node, boolean omitDeclaration) throws Exception { Transformer xformer = TransformerFactory.newInstance().newTransformer(); if (omitDeclaration) { xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); Source source = new DOMSource(node); xformer.transform(source, result); return sw.toString(); }