List of usage examples for javax.xml.transform Transformer setOutputProperty
public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;
From source file:Main.java
public static String getStringFromXmlDoc(org.w3c.dom.Node node) throws Exception { TransformerFactory tf = TransformerFactory .newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", null); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.getBuffer().toString().replaceAll("\n|\r", ""); }
From source file:Main.java
public static String documentToString(Node document) { try {//w w w.ja v a 2s . c om StringWriter sw = new StringWriter(); 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(document), new StreamResult(sw)); return sw.toString().replace("\r\n", "\n"); } catch (TransformerException e) { throw new IllegalAccessError("Couldn't transform document to string"); } }
From source file:Main.java
public static void print(Document doc) { try {/*from w w w .java 2 s.c o m*/ TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty("encoding", "utf-8"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(System.out, "utf-8"))); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void toWriter(Document doc, Writer writer) { if (doc == null || writer == null) { return;// w w w .j a v a2s. c om } try { Transformer tran = tf.newTransformer(); tran.setOutputProperty(OutputKeys.INDENT, "yes"); Source src = new DOMSource(doc); Result res = new StreamResult(writer); tran.transform(src, res); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static void printDocument(Document doc, OutputStream out) { try {/*from w w w.jav a 2 s .c o m*/ 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", "2"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); } catch (Exception e) { System.out.println("Error while printing the document."); } }
From source file:Main.java
public static void writeDocToFile(Document doc, String filename) throws Exception { Source source = new DOMSource(doc); File file = new File(filename); Result result = new StreamResult(file); TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", 2); Transformer xformer = tFactory.newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); //xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); xformer.transform(source, result);//from w w w . j a v a 2 s . c o m /*OutputFormat format = new OutputFormat(doc); format.setIndenting(true); format.setIndent(2); Writer output = new BufferedWriter( new FileWriter(filename) ); XMLSerializer serializer = new XMLSerializer(output, format); serializer.serialize(doc);*/ }
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 transText(Node doc) throws Exception { TransformerFactory transfactory = TransformerFactory.newInstance(); Transformer transformer = transfactory.newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "yes"); transformer.setOutputProperty("indent", "yes"); Source source = new DOMSource(doc); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); transformer.transform(source, result); return sw.toString(); }
From source file:Main.java
/** * Save document to a file//from w w w .j a v a 2s . c o m * * @param document : Document to be saved * @param fileName : Represent file name to save * @throws Exception */ public static void saveDocumentTo(Document document, String fileName) throws Exception { File encryptionFile = new File(fileName); try (FileOutputStream fOutStream = new FileOutputStream(encryptionFile)) { TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(fOutStream); transformer.transform(source, result); } }
From source file:Main.java
public static void writeXml(OutputStream os, Node node, String encoding) throws TransformerException { TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); DOMSource source = new DOMSource(); source.setNode(node);//w w w .j a v a 2 s. c om StreamResult result = new StreamResult(); result.setOutputStream(os); transformer.transform(source, result); }