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 void saveXMLDocumentAsFile(Document doc, File file) { try {/* w ww . j a v a 2 s. c o m*/ // Indent & Doctype declaration Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); // Prepares to write StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); // Output to file result = new StreamResult(file); transformer.transform(source, result); } catch (Exception e) { System.out.println("Error on saving XML file."); e.printStackTrace(); } }
From source file:Main.java
public static void xmlToStream(Node n, OutputStream os) throws Exception { Source source = new DOMSource(n); // PrintWriter pr = new PrintWriter(new OutputStreamWriter(os,StandardCharsets.UTF_8),true); Result result = new StreamResult(os);//pr); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); }
From source file:Main.java
public static void xmlToStream(Node n, Writer writer) throws Exception { Source source = new DOMSource(n); // PrintWriter pr = new PrintWriter(new OutputStreamWriter(os,StandardCharsets.UTF_8),true); Result result = new StreamResult(writer);//pr); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); }
From source file:Main.java
public static String formatDOM(Node node) throws TransformerException { StringWriter writer = new StringWriter(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; transformer = tf.newTransformer();//from w w w . j a v a 2 s . com transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // encoding doesn't matter since we stick with strings transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
/** * To output a DOM as a stream.//from ww w .ja v a 2 s.c o m */ public static InputStream documentToPrettyInputStream(Node document) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(document); Result outputTarget = new StreamResult(outputStream); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(xmlSource, outputTarget); InputStream is = new ByteArrayInputStream(outputStream.toByteArray()); return is; }
From source file:Main.java
public static String xmlToString(Document doc) { try {// w w w . j av a2s . c o m Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return result.getWriter().toString(); } catch (TransformerException ex) { System.err.println("Error: Cannot load xml transformer"); System.err.println("Cause: " + ex.toString()); System.exit(1); return null; } }
From source file:Main.java
public static void xmlToStreamE(Node n, OutputStream os) { try {//from www . jav a 2s .c o m Source source = new DOMSource(n); // PrintWriter pr = new PrintWriter(new OutputStreamWriter(os,StandardCharsets.UTF_8),true); Result result = new StreamResult(os);//pr); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(source, result); } catch (IllegalArgumentException | TransformerException e) { throw new Error(e); } }
From source file:Main.java
/** * Node to string./*from ww w . jav a 2 s . c o m*/ * * @param node the node * @return the string */ public static String nodeToString(final Node node) { final StringWriter sw = new StringWriter(); try { final Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (final TransformerException te) { System.out.println("nodeToString Transformer Exception"); } return sw.toString(); }
From source file:Main.java
private static void transform(Document doc, Result result) throws Exception { try {//from w w w.j av a2 s. c om // Prepare the DOM document for writing Source source = new DOMSource(doc); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); throw e; } catch (TransformerException e) { e.printStackTrace(); throw e; } catch (Exception e) { e.printStackTrace(); throw e; } }
From source file:Main.java
/** * Format the provided XML input./* w w w .j a va 2 s . c om*/ * * @param input * XML input to format. * @param indent * Indentation to use on formatted XML. * @return Formatted XML. * @throws TransformerException * if some problem occur while processing the xml * @see #prettyFormat(String) */ public static String prettyFormat(String input, Integer indent) throws TransformerException { if (input != null) { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent == null ? 2 : indent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } return input; }