List of usage examples for javax.xml.transform Transformer setOutputProperty
public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;
From source file:Main.java
/** * Creates and configures Transformer object * @return Transformer object or null on failure * @throws TransformerConfigurationException If cannot create *//* w ww. jav a 2 s .c om*/ public static Transformer createTransformer() throws TransformerConfigurationException { if (s_transformerFactory == null) { synchronized (TransformerFactory.class) { if (s_transformerFactory == null) { s_transformerFactory = TransformerFactory.newInstance(); } } } Transformer transformer = s_transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ return transformer; }
From source file:Main.java
public static String prettyPrint(DOMSource domSource) { try {/*from w ww.j av a 2 s. c om*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult streamResult = new StreamResult(new StringWriter()); transformer.transform(domSource, streamResult); return streamResult.getWriter().toString(); } catch (Exception e) { throw new RuntimeException("Error while pretty printing xml", e); } }
From source file:Main.java
/** Write a DOM to a file. */ public static void writeXML(Document document, File file) throws IOException { // save the DOM to file StreamResult result = new StreamResult(new BufferedOutputStream(new FileOutputStream(file))); TransformerFactory transFactory = TransformerFactory.newInstance(); try {// w w w . j a va2 s .co m Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); transformer.transform(new DOMSource(document), result); } catch (TransformerConfigurationException ex) { throw new IOException(ex.getMessage()); } catch (TransformerException ex) { throw new IOException(ex.getMessage()); } result.getOutputStream().close(); }
From source file:Main.java
public static String formatXML(String xml) { try {//from ww w.j av a 2 s . c o m Transformer serializer = SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()); } catch (Exception e) { return xml; } }
From source file:Main.java
public static void transformNonTextNodeToOutputStream(Node node, OutputStream os, boolean omitXmlDeclaration) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); if (omitXmlDeclaration) { trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); }// w w w. j a v a 2s. co m trans.transform(new DOMSource(node), new StreamResult(os)); }
From source file:Main.java
public static String toString(final Document document) { try {/*from ww w.j a va 2s . co m*/ final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.STANDALONE, "yes"); //http://johnsonsolutions.blogspot.ca/2007/08/xml-transformer-indent-doesnt-work-with.html transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(document), new StreamResult(baos)); final String result = baos.toString(); return result; } catch (final TransformerException e) { throw new Error(e); } }
From source file:Main.java
/** ask transformer to pretty-print the output: works with Java built-in XML engine * @param transformer will add indent property *///from w w w . j av a 2 s . c o m public static void setTransformerIndent(final Transformer transformer) { try { transformer.setOutputProperty(XSLT_INDENT_PROP, "4"); } catch (final IllegalArgumentException e) { System.err.println("indent-amount not supported: {}" + e.toString()); // ignore error, don't print stack-trace } }
From source file:Main.java
private static String toString(final Document document, final boolean indent, final boolean omitXmlDeclaration) throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitXmlDeclaration ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); final StringWriter stringWriter = new StringWriter(); final StreamResult streamResult = new StreamResult(stringWriter); transformer.transform(new DOMSource(document), streamResult); return stringWriter.toString(); }
From source file:Main.java
public static String prettyPrint(Node node) { try {/* w ww . j av a 2 s . c o m*/ Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }
From source file:Main.java
public static void document2File(Document document, String encode, File file) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty("encoding", encode); transformer.setOutputProperty("indent", "yes"); transformer.transform(source, new StreamResult(file)); }