List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
public static void serialize(Document doc, OutputStream out, Transformer transformer) throws TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(out); transformer.transform(source, result); }
From source file:Main.java
public static byte[] xmlToBytes(final Node body) throws TransformerException { final Transformer transformer = FACTORY.newTransformer(); final Source source = new DOMSource(body); final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); final Result result = new StreamResult(buffer); transformer.transform(source, result); return buffer.toByteArray(); }
From source file:Main.java
public static void saveDoc(Document docToSave, String filePath) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(docToSave); StreamResult result = new StreamResult(new File(filePath)); transformer.transform(source, result); }
From source file:Main.java
public static StringBuffer transfer(Element element, StreamSource source) { try {/*from w ww.java 2 s .co m*/ StringWriter sw = new StringWriter(); Transformer trans = null; if (source != null) trans = TransformerFactory.newInstance().newTransformer(source); else trans = TransformerFactory.newInstance().newTransformer(); trans.transform(new DOMSource(element), new StreamResult(sw)); sw.close(); return sw.getBuffer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String prettyPrint(DOMSource domSource) { try {//www .j a va 2 s.c om Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult streamResult = new StreamResult(new StringWriter()); transformer.transform(domSource, streamResult); return streamResult.getWriter().toString(); } catch (Exception e) { throw new RuntimeException("Error while pretty printing xml", e); } }
From source file:Main.java
/** * @param xml//from w w w .j ava 2 s . c o m */ public static String prettyPrintToString(String xml) { Source xmlInput = new StreamSource(new StringReader(xml)); StreamResult xmlOutput = new StreamResult(new StringWriter()); Transformer transformer = null; try { transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "testing.dtd"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); try { transformer.transform(xmlInput, xmlOutput); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } String formattedxml = xmlOutput.getWriter().toString(); return formattedxml; }
From source file:Main.java
/** * Converts a {@link Node node} to an XML string * * @param node the first element/*w w w . j a v a 2 s .c o m*/ * @return the XML String representation of the node, never null */ public static String nodeToString(Node node) { try { StringWriter writer = new StringWriter(); createIndentingTransformer().transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); } catch (TransformerException e) { throw new IllegalStateException(e); } }
From source file:Main.java
public static byte[] doc2bytes(Document doc, boolean formated) { try {// ww w . j a va 2 s . c om Source source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); Result result = new StreamResult(out); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); if (formated) { // linefeed formatting transformer.setOutputProperty(OutputKeys.INDENT, "yes"); } else { // remove xml header transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(source, result); return out.toByteArray(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String prettifyXmlString(String uglyXml) { Transformer transformer = null; try {/* w ww . j a v a 2 s. c o m*/ transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = null; try { source = new DOMSource(string2Document(uglyXml)); } catch (Exception e) { e.printStackTrace(); } try { transformer.transform(source, result); } catch (TransformerException e) { e.printStackTrace(); } String xmlString = result.getWriter().toString(); return xmlString; }
From source file:Main.java
public static String sourceToString(Source source) throws IOException { try {/*from w ww . jav a 2 s . co m*/ Transformer trans = transFactory.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); Writer writer = new StringWriter(); trans.transform(source, new StreamResult(writer)); writer.flush(); return writer.toString(); } catch (TransformerException ex) { throw new IOException(ex); } }