List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
/** * Writes a {@link Document} as text to the given {@link Writer}. * @param doc the document to output/*w ww .j a va 2s .c o m*/ * @param out the writer to output to */ public static void writeDocument(Document doc, Writer out) { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = transformerFactory.newTransformer(); } catch (final TransformerConfigurationException e) { throw new RuntimeException(e); // TODO proper exception handling } DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(out); try { transformer.transform(source, result); } catch (final TransformerException e) { throw new RuntimeException(e); // TODO proper exception handling } }
From source file:Main.java
public static String getXMLAsString(Document doc) throws Exception { 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(); }
From source file:Main.java
/** * Print a DOM tree to an output stream or if there is an exception while doing so, print the * stack trace./*from w ww . ja v a 2 s.c o m*/ * * @param dom * @param os */ public static void printDom(Node dom, OutputStream os) { Transformer trans; PrintWriter w = new PrintWriter(os); try { TransformerFactory fact = TransformerFactory.newInstance(); trans = fact.newTransformer(); trans.transform(new DOMSource(dom), new StreamResult(new OutputStreamWriter(os))); } catch (TransformerException e) { w.println("An error ocurred while transforming the given DOM:"); e.printStackTrace(w); } }
From source file:Main.java
/** * method used to convert a xml document to a string * //from w w w . j av a2 s. c o m * @param doc * @return */ public static String convertDocumentToString(Document doc) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; try { /** * transformation of document happens here */ transformer = tf.newTransformer(); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString(); /** * logging success */ logger.info("xml conversion to string successful Class:XMLParserUtility line# 98"); return output; } catch (TransformerException e) { e.printStackTrace(); logger.fatal("Could not transform document to XML", e); } return null; }
From source file:Main.java
public static String prettyPrintXML(String xml, int indentAmount) throws TransformerConfigurationException, TransformerException { Transformer serializer = SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indentAmount)); Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()); }
From source file:Main.java
/** * Save document to a file/* w w w . jav a 2 s.co m*/ * * @param document : Document to be saved * @param fileName : Represent file name to save * @throws Exception */ public static void saveDocumentTo(Document document, String fileName) throws Exception { File encryptionFile = new File(fileName); try (FileOutputStream fOutStream = new FileOutputStream(encryptionFile)) { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(fOutStream); transformer.transform(source, result); } }
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 String printDocument(Node doc) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter sw = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static String printDocument(Node doc) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter sw = new StringWriter(); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
/** * Transform xml Document to String/* www . j ava2 s . c o m*/ * @param doc Xml Document * @return String representation */ public static String getContent(final Document doc) { try { final StringWriter sw = new StringWriter(); final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); } catch (Exception ex) { throw new RuntimeException("Error converting to String", ex); } }