List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:Main.java
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); 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.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
public static void createFile(Document document, String filePath) throws TransformerException { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(document); Result dest = new StreamResult(new File(filePath)); aTransformer.transform(src, dest);//from w w w . ja va 2 s . c o m }
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 void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "no"); transformer.setOutputProperty("method", "xml"); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty("encoding", "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
public static String formatDocument2(Document document) throws TransformerException { DOMSource domSource = new DOMSource(document); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); return new String(out.toByteArray()); }
From source file:Main.java
public static String document2String(Document document, String encode) throws TransformerException { String xml = null;/*w ww . ja v a2 s.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
/** * Print a DOM tree to an output stream or if there is an exception while doing so, print the * stack trace./*from ww w . j a v a 2s.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
public static StringWriter getStringWriter(Document doc) throws TransformerException { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult streamResult = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, streamResult); return writer; }
From source file:Main.java
public static void writeXml(Document dom, OutputStream os) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(dom); StreamResult result = new StreamResult(os); transformer.transform(source, result); }
From source file:Main.java
public static void saveDocument(Document doc, File file) throws TransformerConfigurationException, TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(file); transformer.transform(source, result); }