List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
public static void printDom(Document dom, String filename) { try {/* w ww . j a v a 2s . c o m*/ DOMSource source = new DOMSource(dom); StreamResult result; result = new StreamResult(new FileOutputStream(filename)); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.transform(source, result); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String getXmlFileToString(String filePath) throws TransformerException { File xmlFile = new File(filePath); StringWriter writer = new StringWriter(); TransformerFactory fac = TransformerFactory.newInstance(); Transformer x = fac.newTransformer(); x.transform(new StreamSource(xmlFile), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
public static String transformToString(Source source) { try {//from www . j a v a 2 s . com Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new StringWriter()); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerException ex) { ex.printStackTrace(); return null; } }
From source file:Main.java
static public void toXml(Document xmldoc, OutputStream os) throws TransformerException { StreamResult out = new StreamResult(os); DOMSource domSource = new DOMSource(xmldoc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, out); }
From source file:Main.java
public static String trans(Element ele) { StringWriter writer = new StringWriter(); try {//w ww . j ava 2 s . co m transformer.transform(new DOMSource(ele), new StreamResult(writer)); } catch (TransformerException e) { e.printStackTrace(); } return writer.toString(); }
From source file:Main.java
public static String toStringFromDoc(Element elem) { String result = null;//from w w w.ja va 2 s .c om StringWriter strWtr = new StringWriter(); StreamResult strResult = new StreamResult(strWtr); TransformerFactory tfac = TransformerFactory.newInstance(); try { javax.xml.transform.Transformer t = tfac.newTransformer(); t.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); // xml, html, // text t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(new DOMSource(elem), strResult); } catch (Exception e) { System.err.println("XML.toString(Document): " + e); } result = strResult.getWriter().toString(); try { strWtr.close(); } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static String transform(Source xsl, Document doc) { StringWriter writer = new StringWriter(); try {//from ww w. j av a 2s. c om TransformerFactory.newInstance().newTransformer(xsl).transform(new DOMSource(doc), new StreamResult(writer)); } catch (Exception e) { } return writer.toString(); }
From source file:Main.java
public static void serializeXML(Node e, Writer out) throws Exception { DOMSource domSource = new DOMSource(e); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); // turn off <?xml...?> stuff as for documents that were parsed with // non-UTF8 encoding, serializer inserts encoding="[non-utf-8]" there which // it should not, since we always serialize as UTF-8 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); }
From source file:Main.java
static String getXMLString(Document xmlDoc) throws Exception { StringWriter writer = null;//from w w w. ja v a2s . c om DOMSource source = new DOMSource(xmlDoc.getDocumentElement()); writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.transform(source, result); StringBuffer strBuf = writer.getBuffer(); return strBuf.toString(); }
From source file:Main.java
public static String xmlToString(Document doc) { try {// w w w .ja v a 2 s . c o m Transformer transformer = TransformerFactory.newInstance().newTransformer(); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerException ex) { ex.printStackTrace(); } return null; }