List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
public static void save(Document document, IFile file, IProgressMonitor monitor) throws TransformerException, CoreException, UnsupportedEncodingException { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.ENCODING, "utf-8"); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult sr = new StreamResult(sw); DOMSource ds = new DOMSource(document); trans.transform(ds, sr);/*from ww w .j av a2s. c om*/ String xmlString = sw.toString(); InputStream is = new ByteArrayInputStream(xmlString.getBytes("UTF-8")); if (file.exists()) { file.setContents(is, 0, monitor); } else { file.create(is, true, monitor); } }
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. *//*www .j a v a2 s. com*/ 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 getDocumentAsString(Document doc) throws TransformerException { StringWriter sw = new StringWriter(); tf.newTransformer().transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static void writeXml(Document doc, File output) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(output); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(source, result); }
From source file:Main.java
public static String prettyPrint(Node node) { try {//from w ww .jav a2 s .c om Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void writeDocument(OutputStream iStream, Element iElement) throws TransformerException { TransformerFactory lTransformFactory = TransformerFactory.newInstance(); Transformer lTransformer = lTransformFactory.newTransformer(); DOMSource lSource = new DOMSource(iElement); StreamResult lStreamResult = new StreamResult(iStream); lTransformer.transform(lSource, lStreamResult); }
From source file:Main.java
/** Write a DOM document to a string */ public static String xmlDocumentToString(Document document) { try {/*from ww w . j a v a2s .c om*/ 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(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static boolean write(String filename, Document document, boolean addDocType) { try {//from w w w.j a v a 2s.c o m TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", new Integer(4)); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); if (addDocType) { transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://java.sun.com/dtd/facelet-taglib_1_0.dtd"); } transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, new StreamResult(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename))))); return true; } catch (Exception e) { return false; } }
From source file:Main.java
public static String saveInString(Document doc) { StreamResult streamResult = new StreamResult(new StringWriter()); save(doc, streamResult);//from w w w. j a v a 2 s . c o m return streamResult.getWriter().toString(); }
From source file:Main.java
public static void xmlToStreamE(Node n, OutputStream os) { try {//w w w .j av a 2 s .co m Source source = new DOMSource(n); // PrintWriter pr = new PrintWriter(new OutputStreamWriter(os,StandardCharsets.UTF_8),true); Result result = new StreamResult(os);//pr); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); } catch (IllegalArgumentException | TransformerException e) { throw new Error(e); } }