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
public static void doc2stream(Document doc, StreamResult result) throws TransformerConfigurationException, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); transformer.transform(source, result); }
From source file:Main.java
public static String formatXmlAsStringNoWhitespace(Document document) { StringWriter writer = new StringWriter(); TransformerFactory factory = TransformerFactory.newInstance(); try {//from ww w. ja va 2 s . c o m Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.transform(new DOMSource(document), new StreamResult(writer)); } catch (TransformerException ex) { throw new RuntimeException("Error formatting xml as no-whitespace string", ex); } return writer.toString(); }
From source file:Main.java
public static String nodeToString(Node node) { StringWriter sw = new StringWriter(); try {/*from w w w. j a v a 2s .c om*/ Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.transform(new DOMSource(node), new StreamResult(sw)); } catch (TransformerException te) { throw new RuntimeException(te); } return sw.toString(); }
From source file:Main.java
public static String documentToString(Node document) { try {/*w w w . ja v a 2 s .c o m*/ 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 serialize(Document doc, OutputStream out) throws Exception { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer;/*www . j av a 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(doc), new StreamResult(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 String createPrintableString(Document doc) { String s = ""; try {//from www .j a v a 2 s.c om // set up a transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); s = sw.toString(); } catch (Exception e) { e.printStackTrace(); } return s; }
From source file:Main.java
public static String xml2Str(Document document) { try {//from www.j ava2 s. co m DOMSource source = new DOMSource(document); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(source, result); return (writer.getBuffer().toString()); } catch (Exception e) { e.printStackTrace(); } return ""; }
From source file:Main.java
/** * @return a transformer that indents entries by 4 characters (never null) */// w ww . j a v a2 s .co m public static Transformer createIndentingTransformer() { Transformer transformer; try { TRANSFORMER_FACTORY.setAttribute("indent-number", 4); transformer = TRANSFORMER_FACTORY.newTransformer(); } catch (final Exception e) { throw new IllegalStateException(e); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); return transformer; }
From source file:Main.java
/** * output XML data in a file/* w ww.j a va 2 s . c o m*/ * * @param doc * @param path */ public static void buildXmlFile(Document doc, String path) { TransformerFactory tfactory = TransformerFactory.newInstance(); try { Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("encoding", "UTF-8"); transformer.transform(source, result); } catch (TransformerConfigurationException e) { //TODO } catch (TransformerException e) { } }
From source file:Main.java
private static void finalizeXML(File xmlFile, Document doc, int intendAmount) throws Exception { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(xmlFile); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "" + intendAmount); transformer.setOutputProperty("indent", "yes"); transformer.transform(source, result); }