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 format(Source source, boolean omitDeclaration, Result result) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); try {//from ww w.ja v a 2 s.c o m transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); } catch (IllegalArgumentException exc) { // not supported } if (omitDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(source, result); }
From source file:Main.java
public static String getXMLAsString(Document doc) throws Exception { 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(); }
From source file:Main.java
/** * DOM to string./* www.j a v a 2s .co m*/ * * @param doc * the doc * @return the string */ /* * from: * http://www.journaldev.com/71/utility-java-class-to-format-xml-document * -to-xml-string-and-xml-to-document */ public static String DOMToString(Document doc) { String xmlString = ""; if (doc != null) { try { TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); xmlString = sw.toString(); } catch (Exception e) { e.printStackTrace(); } } return xmlString; }
From source file:Main.java
/** * Convert the document to an array of bytes. * /*from w w w . j a v a2 s . c o m*/ * @param doc * The XML document. * @param encoding * The encoding of the output data. * * @return The XML document as an array of bytes. * * @throws TransformerException * If there is an error transforming to text. */ public static byte[] asByteArray(Document doc, String encoding) throws TransformerException { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); final StringWriter writer = new StringWriter(); final Result result = new StreamResult(writer); final DOMSource source = new DOMSource(doc); transformer.transform(source, result); return writer.getBuffer().toString().getBytes(); }
From source file:Main.java
public static boolean ExportXML(Document xmlOutput, String filename) { try {//from w w w . jav a 2 s .c om if (xmlOutput != null) { Source source = new DOMSource(xmlOutput); FileOutputStream file = new FileOutputStream(filename); StreamResult res = new StreamResult(file); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, "ISO8859-1"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, res); file.close(); return true; } return false; } catch (FileNotFoundException e) { return false; } catch (TransformerConfigurationException e) { return false; } catch (TransformerException e) { return false; } catch (IOException e) { return false; } }
From source file:Main.java
/** * /*from w w w . j av a2 s .c om*/ * @param xml * @param indent * @return pretty formatted xml */ public static String prettyFormat(String xml, int indent) { try { Source xmlInput = new StreamSource(new StringReader(xml)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { throw new RuntimeException(e); // simple exception handling, please review it } }
From source file:Main.java
static public void toXml(Document xmldoc, OutputStream os) throws TransformerException { StreamResult out = new StreamResult(os); DOMSource domSource = new DOMSource(xmldoc); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(domSource, out); }
From source file:Main.java
public static void print(Node node, OutputStream out) throws UnsupportedEncodingException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); 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", "2"); transformer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
/** This method dumps out a dom document to an output stream. * Header information is turned off./*from www .j av a 2 s . c om*/ * * @param doc Dom Document * @param os existing outputstream you wish to write the document to. */ public static void DOMtoOutputStream(Document doc, OutputStream os) { try { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); DOMSource src = new DOMSource(doc); StreamResult result = new StreamResult(os); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(src, result); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static String writeDocumentToString(Document document) { try {/* w w w. j a v a 2 s. com*/ TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans; trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StringWriter writer = new StringWriter(); try { StreamResult streamResult = new StreamResult(writer); DOMSource source = new DOMSource(document); trans.transform(source, streamResult); } catch (TransformerException e) { e.printStackTrace(); return null; } finally { writer.close(); } return writer.toString(); } catch (TransformerConfigurationException | IOException e) { e.printStackTrace(); return null; } }