List of usage examples for javax.xml.transform TransformerFactory newTransformer
public abstract Transformer newTransformer() throws TransformerConfigurationException;
From source file:Main.java
public static String Document2String(Document doc) throws IOException, TransformerException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final TransformerFactory tf = TransformerFactory.newInstance(); final 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(baos, "UTF-8"))); return baos.toString("UTF-8"); }
From source file:Main.java
public static String prettyPrintXML(String source) { try {//from w w w .j av a 2 s . c o m ByteArrayOutputStream baos = new ByteArrayOutputStream(); TransformerFactory tff = TransformerFactory.newInstance(); Transformer tf; tf = tff.newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(baos); tf.transform(new StreamSource(new StringReader(source)), result); return new String(baos.toByteArray()); } catch (Exception e) { return source; } }
From source file:Main.java
public static void writeDocument(Document document, File file) throws TransformerException { TransformerFactory transformFactory = TransformerFactory.newInstance(); Transformer idTransform = transformFactory.newTransformer(); Source input = new DOMSource(document); Result output = new StreamResult(file); idTransform.transform(input, output); }
From source file:Main.java
public static void writeDocument(Document document, OutputStream out) throws TransformerException { TransformerFactory transformFactory = TransformerFactory.newInstance(); Transformer idTransform = transformFactory.newTransformer(); Source input = new DOMSource(document); Result output = new StreamResult(out); idTransform.transform(input, output); }
From source file:Main.java
public static void nodeToWriter(Node node, Writer writer) throws Exception { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "no"); DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(writer); transformer.transform(source, result); }
From source file:Main.java
public static String xmlDocumentToString(final Document doc) { try {/* www. j a v a 2 s . c om*/ final DOMSource domSource = new DOMSource(doc); final StringWriter writer = new StringWriter(); final StreamResult result = new StreamResult(writer); final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.transform(domSource, result); return writer.toString(); } catch (final TransformerException ex) { ex.printStackTrace(); return null; } }
From source file:Main.java
public static String returnDocAsString(Document doc) throws TransformerException { StringWriter sw = new StringWriter(); TransformerFactory tfFac = TransformerFactory.newInstance(); // use null trandformation Transformer tf = tfFac.newTransformer(); tf.transform(new DOMSource(doc), new StreamResult(sw)); return sw.toString(); }
From source file:Main.java
public static void printNode(Node node) { try {// ww w . j a v a 2s . c o m // Set up the output transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // Print the DOM node StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(node); trans.transform(source, result); String xmlString = sw.toString(); System.out.println(xmlString); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
/** * Print the specified XML DOM node to the specified output stream * /*from w ww. j av a 2 s .c o m*/ * @param n * the node to print * @param os * the output stream to print to * @throws Exception */ public static void printNode(Node n, OutputStream os) throws Exception { TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(new DOMSource(n), new StreamResult(os)); }
From source file:Main.java
public static void dumpElement(Logger log, Element elem) { try {//from w ww . j a v a2s . c o m TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(elem), new StreamResult(buffer)); log.debug(buffer.toString()); } catch (TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException ex) { log.error("Failed to dump element", ex); } }