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 writeDocument(Document document, String location) { DOMSource source = new DOMSource(document); File file = new File(location); Result result = new StreamResult(file); Transformer xformer;/*from w w w. ja v a 2 s . com*/ try { xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); xformer.setOutputProperty(OutputKeys.METHOD, "xml"); xformer.transform(source, result); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
public static String prettyPrint(File file) { Transformer tf;/*from w w w . j av a 2 s . c o m*/ try { tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult stringResult = new StreamResult(new StringWriter()); tf.transform(new DOMSource(convertFileToDom(file)), stringResult); return stringResult.getWriter().toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static void printDocument(final Document doc) { try {/* w w w . j a va 2 s . co m*/ final TransformerFactory tf = TransformerFactory.newInstance(); Transformer 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(doc), new StreamResult(new OutputStreamWriter(System.out, "UTF-8"))); } catch (final TransformerConfigurationException e) { e.printStackTrace(); } catch (final UnsupportedEncodingException e) { e.printStackTrace(); } catch (final TransformerException e) { e.printStackTrace(); } }
From source file:Main.java
public static void toWriter(Document doc, Writer writer) { if (doc == null || writer == null) { return;// w w w. j a v a 2s. c o m } try { Transformer tran = tf.newTransformer(); tran.setOutputProperty(OutputKeys.INDENT, "yes"); Source src = new DOMSource(doc); Result res = new StreamResult(writer); tran.transform(src, res); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
public static String Document2String(Document doc) throws IOException, TransformerException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final TransformerFactory tf = TransformerFactory.newInstance(); final 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(doc), new StreamResult(new OutputStreamWriter(baos, "UTF-8"))); return baos.toString("UTF-8"); }
From source file:Main.java
/** * Convert the document to an array of bytes. * * @param doc The XML document./*w w w. ja va 2s . co m*/ * @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 { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); DOMSource source = new DOMSource(doc); transformer.transform(source, result); return writer.getBuffer().toString().getBytes(); }
From source file:Main.java
/** * Get DOM as a string/*ww w .j a v a2 s . c om*/ * @param doc * @return */ public static String getStringFromDoc(org.w3c.dom.Document doc) { if (doc == null) { System.out.println("XML document is null"); } try { DOMSource domSource = new DOMSource(doc); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://commons.omniupdate.com/dtd/standard.dtd"); transformer.transform(domSource, result); writer.flush(); return writer.toString(); } catch (TransformerException ex) { System.out.println("Transformer Exception"); // ex.printStackTrace(); return "Error in transformation"; } }
From source file:Main.java
/** * Convert XML Node to String// w w w . ja va2 s .c o m * * @param node * the node to convert * @return the String equivalent of the node * @throws TransformerException */ public static String nodeToString(final Node node) throws TransformerException { final Transformer tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); final Writer out = new StringWriter(); tf.transform(new DOMSource(node), new StreamResult(out)); return out.toString(); }
From source file:Main.java
/** * Formatte un XML/*from w w w . ja v a 2 s.com*/ * */ public static String prettyFormat(String input, int indent) { try { Source xmlInput = new StreamSource(new StringReader(input)); 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 (Throwable e) { try { Source xmlInput = new StreamSource(new StringReader(input)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Throwable t) { return input; } } }
From source file:Main.java
/** * Updates existing file with Document object * /* ww w . j a v a 2 s . co m*/ * @param doc * xml document * @param file * xml file to update * @throws TransformerFactoryConfigurationError */ public static void updateXMLFile(Document doc, File file) throws TransformerFactoryConfigurationError { try { DOMSource source = new DOMSource(doc); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult result = new StreamResult(file); transformer.transform(source, result); } catch (Exception e) { throw new RuntimeException("Exception while saving message to xml file", e); } }