List of usage examples for javax.xml.transform OutputKeys INDENT
String INDENT
To view the source code for javax.xml.transform OutputKeys INDENT.
Click Source Link
From source file:Main.java
/** * To output a DOM as a stream.//from w ww . j a v a 2 s. com */ 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
/** * @param doc//from ww w . j a v a 2s . c o m * @return * @throws RuntimeException */ public static String prettyPrint(Document doc) throws RuntimeException { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; StringWriter writer = new StringWriter(); try { serializer = tfactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //$NON-NLS-1$ //$NON-NLS-2$ serializer.transform(new DOMSource(doc), new StreamResult(writer)); return writer.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void writeXml(Document document, OutputStream out) { try {/*from www . j a va2s .c om*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer; serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource domSource = new DOMSource(document); StreamResult streamResult = new StreamResult(out); serializer.transform(domSource, streamResult); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * To output a DOM as a stream./*w w w. j a v a 2s . c om*/ */ public static InputStream documentToPrettyInputStream(Document 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 void prettyPrint(Document document) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer;/*from w w w .j a va 2 s.c om*/ try { serializer = tfactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); serializer.transform(new DOMSource(document), new StreamResult(System.out)); } catch (TransformerException e) { // this is fatal, just dump the stack and throw a runtime exception e.printStackTrace(); throw new RuntimeException(e); } }
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
/** * Writes the given document to the given writer. * * @param document the document to write * @param writer receives the written document * @param indent number of spaces to indent, null means don't indent *///from ww w .j a va 2 s. c o m public static void writeDocument(Document document, Writer writer, Integer indent) { TransformerFactory tf = TransformerFactory.newInstance(); try { Transformer trans = tf.newTransformer(); if (indent != null) { trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent)); } trans.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerConfigurationException e) { throw new RuntimeException(e); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Convert a DOM tree into a String using transform * @param domDoc DOM object * @throws java.io.IOException I/O exception * @return XML as String *//*from ww w.java2s . c o m*/ public static String docToString2(Document domDoc) throws IOException { try { TransformerFactory transFact = TransformerFactory.newInstance(); Transformer trans = transFact.newTransformer(); trans.setOutputProperty(OutputKeys.INDENT, "no"); StringWriter sw = new StringWriter(); Result result = new StreamResult(sw); trans.transform(new DOMSource(domDoc), result); return sw.toString(); } catch (Exception ex) { throw new IOException(String.format("Error converting from doc to string %s", ex.getMessage())); } }
From source file:Main.java
public static void writeXmlFile(Document doc, File file, boolean indent, String encoding) throws TransformerFactoryConfigurationError, TransformerException { // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file Result result = new StreamResult(file); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, encoding); if (indent) { xformer.setOutputProperty(OutputKeys.INDENT, "yes"); }//from ww w .ja va2 s.co m xformer.transform(source, result); }
From source file:Main.java
private static String convertDOMToString(DOMSource source, boolean outputXmlDeclaration) { StreamResult result = null;//from w ww . j ava2 s . c om try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (!outputXmlDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } result = new StreamResult(new StringWriter()); transformer.transform(source, result); } catch (TransformerException e) { } return result.getWriter().toString(); }