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 writeXml(Document doc, File output) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(output); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(source, result); }
From source file:Main.java
/** * /* ww w. j a v a 2 s . c o m*/ * @param header Just a title for the stanza for readability. Single word no spaces since * it is inserted as the root element in the output. * @param xml The string to pretty print */ static public void prettyPrint(String header, String xml) { try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); if (header != null) { xml = "\n<" + header + ">" + xml + "</" + header + '>'; } transformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(System.out)); } catch (Exception e) { System.out.println("Something wrong with xml in \n---------------\n" + xml + "\n---------------"); e.printStackTrace(); } }
From source file:Main.java
public static void writeXmlFile(Document doc, File file) throws TransformerConfigurationException, TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(file); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(source, result); }
From source file:Main.java
public static String getDocumentAsString(Node node, boolean prettyXml) throws TransformerException, UnsupportedEncodingException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); if (prettyXml) { transformer.setOutputProperty(OutputKeys.INDENT, "yes"); }//from w w w . j a v a 2 s . c o m transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); transformer.transform(new DOMSource(node), new StreamResult(baos)); return baos.toString("UTF-8"); }
From source file:Main.java
public static void writeToFile(Document doc, String file) throws Exception { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(doc); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); PrintWriter pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); StreamResult result = new StreamResult(pw); transformer.transform(source, result); }
From source file:Main.java
public static String formatXML(String unformatted) throws SAXException, IOException, TransformerException, ParserConfigurationException { if (unformatted == null) return null; // remove whitespaces between xml tags String unformattedNoWhiteSpaces = unformatted.toString().replaceAll(">[\\s]+<", "><"); // Instantiate transformer input Source xmlInput = new StreamSource(new StringReader(unformattedNoWhiteSpaces)); StreamResult xmlOutput = new StreamResult(new StringWriter()); // Configure transformer Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(xmlInput, xmlOutput); String formatted = xmlOutput.getWriter().toString(); return formatted; }
From source file:Main.java
public static void saveDocument(final Document document, String filename) throws TransformerException { 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"); DOMSource source = new DOMSource(document); StreamResult result = new StreamResult(new File(filename)); t.transform(source, result);//from w w w.j av a2s. c om }
From source file:Main.java
public static void printNode(Node node, String fn) { try {/* w ww. ja v a 2 s. c om*/ TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer; transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource source = new DOMSource(node); PrintStream out = System.out; if (fn != null) out = new PrintStream(new FileOutputStream(fn)); StreamResult result = new StreamResult(out); transformer.transform(source, result); out.close(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }
From source file:Main.java
public static final void prettyPrint(Document xml) throws Exception { Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); Writer out = new StringWriter(); tf.transform(new DOMSource(xml), new StreamResult(out)); System.out.println(out.toString()); }
From source file:Main.java
/** * Uses a TransformerFactory with an identity transformation to convert a * Document into a String representation of the XML. * * @param document Document.//from ww w . ja va2 s . c om * @return An XML String. * @throws IOException if an error occurs during transformation. */ public static String documentToString(Document document) throws IOException { String xml = null; try { DOMSource dom = new DOMSource(document); StringWriter writer = new StringWriter(); StreamResult output = new StreamResult(writer); // Use Transformer to serialize a DOM TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); // No need for pretty printing transformer.setOutputProperty(OutputKeys.INDENT, INDENT_XML); // XML Declarations unexpected whitespace for legacy AS XMLDocument type, // so we always omit it. We can't tell whether one was present when // constructing the Document in the first place anyway... transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, OMIT_XML_DECLARATION); transformer.transform(dom, output); xml = writer.toString(); } catch (TransformerException te) { throw new IOException("Error serializing Document as String: " + te.getMessageAndLocation()); } return xml; }