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 printNode(Node node) { try {//w w w . jav a2 s . c o m // Set up the output transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); // Print the DOM node StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(node); trans.transform(source, result); String xmlString = sw.toString(); System.out.println(xmlString); } catch (TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static String prettyPrintXML(String xml) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(new StringWriter()); StreamSource source = new StreamSource(new StringReader(xml)); transformer.transform(source, result); return result.getWriter().toString(); }
From source file:Main.java
public static void writeXmlFile(Document file, String fileName) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(file); StreamResult result = new StreamResult(new File(fileName)); transformer.transform(source, result); }
From source file:Main.java
public static void saveXMLDocumentAsFile(Document doc, File file) { try {/*from w w w . ja v a 2s .co 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 String xmlToString(Node node) { try {/*from w ww . j a v a 2 s .c om*/ Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void saveDoc(Document docToSave, String filePath) throws TransformerException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); DOMSource source = new DOMSource(docToSave); StreamResult result = new StreamResult(new File(filePath)); transformer.transform(source, result); }
From source file:Main.java
public static void printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer;/*from ww w .j a v a 2 s .co m*/ try { serializer = tfactory.newTransformer(); if (prettyPrint) { //Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); } if (!includeXmlDeclaration) { serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } DOMSource xmlSource = new DOMSource(node); StreamResult outputTarget = new StreamResult(out); serializer.transform(xmlSource, outputTarget); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * This method formats the XML file by omitting the XML Declaration and * creating indentations /*from w ww . j a v a2 s .c o m*/ * @param transformer - transformer that is used to process XML * @return a transformer that omits the XML declaration and performs indentations * @author Alex Song */ private static Transformer formatXML(Transformer transformer) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); return transformer; }
From source file:Main.java
/** * Create a {@link Transformer} just the way we like it. * * @return Returns a new {@link Transformer}. *//*from w w w. ja va2 s .co m*/ private static Transformer createTransformer() { final Transformer xform; try { xform = m_xformFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new IllegalStateException(e); } xform.setOutputProperty(OutputKeys.INDENT, "yes"); xform.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); return xform; }
From source file:Main.java
public static String prettifyXmlString(String uglyXml) { Transformer transformer = null; try {// w w w .jav a2 s.co m transformer = TransformerFactory.newInstance().newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); //initialize StreamResult with File object to save to file StreamResult result = new StreamResult(new StringWriter()); DOMSource source = null; try { source = new DOMSource(string2Document(uglyXml)); } catch (Exception e) { e.printStackTrace(); } try { transformer.transform(source, result); } catch (TransformerException e) { e.printStackTrace(); } String xmlString = result.getWriter().toString(); return xmlString; }