List of usage examples for javax.xml.transform Transformer transform
public abstract void transform(Source xmlSource, Result outputTarget) throws TransformerException;
Transform the XML Source
to a Result
.
From source file:Main.java
public static void saveXMLDocument(Document document, File file) { FileOutputStream output = null; try {/*ww w . jav a 2 s . co m*/ output = new FileOutputStream(file); Source source = new DOMSource(document); Result result = new StreamResult(output); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(source, result); } catch (Exception ex) { try { output.close(); } catch (IOException io) { } } finally { try { output.close(); } catch (IOException io) { } } }
From source file:Main.java
protected static Result internalTransform(Document doc, Templates templates, Result r, boolean trace) { StringWriter sw = new StringWriter(); try {/*from w ww .j av a2 s . c o m*/ Transformer transformer = templates.newTransformer(); transformer.transform(new DOMSource(doc), r); sw.close(); return r; } catch (Throwable th) { th.printStackTrace(); return r; } }
From source file:Main.java
/** * print document to string/*from ww w . j a va2s . com*/ * @param doc * @return * @throws TransformerException */ public static String toString(Document doc) throws TransformerException { Transformer tf = tff.newTransformer(); StringWriter writer = new StringWriter(); tf.transform(new DOMSource(doc), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; }
From source file:Main.java
protected static Result internalTransform(InputStream doc, Templates templates, Result r, boolean trace) { StringWriter sw = new StringWriter(); try {/* w ww .j a va 2s .c om*/ Transformer transformer = templates.newTransformer(); transformer.transform(new StreamSource(doc), r); sw.close(); return r; } catch (Throwable th) { th.printStackTrace(); return r; } }
From source file:Main.java
protected static Result internalTransform(Reader doc, Templates templates, Result r, boolean trace) { StringWriter sw = new StringWriter(); try {/*from w w w . ja v a 2s .c om*/ Transformer transformer = templates.newTransformer(); transformer.transform(new StreamSource(doc), r); sw.close(); return r; } catch (Throwable th) { th.printStackTrace(); return r; } }
From source file:Main.java
/** * output result to object//from w ww . jav a 2s. co m * @param ds input DOM Source * @param sr output stream result */ public static void OutputXmlStream(DOMSource ds, StreamResult sr) { Transformer t; try { t = TransformerFactory.newInstance().newTransformer(); t.transform(ds, sr); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Utils.java
public static void writeXml(Node n, OutputStream os) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); // identity/*from w w w . j a v a 2s.c o m*/ Transformer t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(n), new StreamResult(os)); }
From source file:Main.java
public static String documentToString(Document document) { try {//from w w w .j av a2 s . co m TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); StringWriter sw = new StringWriter(); trans.transform(new DOMSource(document), new StreamResult(sw)); return sw.toString(); } catch (TransformerException tEx) { tEx.printStackTrace(); } return null; }
From source file:Main.java
/** * Converts a node to a string.//from ww w. ja va 2 s. c o m * @param node The node to convert. * @return A string representation of the node. */ public static String getStringFromNode(Node node) { DOMSource domSource = new DOMSource(node); ByteArrayOutputStream baos = null; try { baos = new ByteArrayOutputStream(); StreamResult result = new StreamResult(baos); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(domSource, result); baos.flush(); return new String(baos.toByteArray()); } catch (Exception e) { throw new RuntimeException("Failed to stream node to string", e); } finally { try { baos.close(); } catch (Exception e) { } } }
From source file:Main.java
/** * Saves Document instance to XML file to the specified filePath. * // w w w . j a va2 s .c o m * @param xmlDoc - Document instance * @param destPath - dest path where the XML file is to be saved. * @throws Exception */ public static void saveDocument(Document xmlDoc, String destPath) throws Exception { Source xmlSource = new DOMSource(xmlDoc); OutputStream os = new FileOutputStream(destPath); Result result = new StreamResult(os); TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); /* output to file */ transformer.transform(xmlSource, result); }