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
public static void dumpElement(Logger log, Element elem) { try {//from www . jav a 2s . co m TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(elem), new StreamResult(buffer)); log.debug(buffer.toString()); } catch (TransformerFactoryConfigurationError | IllegalArgumentException | TransformerException ex) { log.error("Failed to dump element", ex); } }
From source file:Main.java
public static void printNode(Node node, String fn) { try {//from w w w.j a v a2s . c o m 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
/** * Dump a {@link Document} or {@link Node}-compatible object to the given {@link OutputStream} (e.g. System.out). * * @param _docOrNode {@link Document} or {@link Node} object * @param _outStream {@link OutputStream} to print on * @throws IOException on error/* w ww. ja va 2 s .c om*/ */ public static void printDocument(Node _docOrNode, OutputStream _outStream) throws IOException { if (_docOrNode == null || _outStream == null) { throw new IOException("Cannot print (on) 'null' object"); } TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer; try { 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(_docOrNode), new StreamResult(new OutputStreamWriter(_outStream, "UTF-8"))); } catch (UnsupportedEncodingException | TransformerException _ex) { throw new IOException("Could not print Document or Node.", _ex); } }
From source file:Main.java
/** * Returns an XML representation of the provided node. * * @param node the node to be represented in XML. * * @return a string containing an XML representation of the * provided DOM node.// w ww .jav a 2s . co m */ public static String nodeToXmlString(Node node) { try { Transformer t = TransformerFactory.newInstance().newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(node); StreamResult result = new StreamResult(new StringWriter()); t.transform(source, result); return result.getWriter().toString(); } catch (TransformerException e) { throw new IllegalStateException(e); } catch (TransformerFactoryConfigurationError e) { throw new IllegalStateException(e); } }
From source file:Main.java
/** * /*from w w w . j a va 2 s .c om*/ * <B>Purpose:</B> Writes the XML Document Object to a File * * @param doc * @param file * @throws TransformerException */ public static void writeXmlFile(Node doc, File file) throws TransformerException { Source source = new DOMSource(doc); Result result = new StreamResult(file); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); xformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n"); xformer.setOutputProperty("standalone", "yes"); xformer.transform(source, result); }
From source file:Main.java
public static Transformer generateTransformer(boolean isOmitXmlDeclaration) throws TransformerConfigurationException { Transformer transformer = generateTransformer(); if (isOmitXmlDeclaration) { transformer.setOutputProperty("omit-xml-declaration", "yes"); } else {// w ww. ja v a2 s . c o m transformer.setOutputProperty("omit-xml-declaration", "no"); } return transformer; }
From source file:Main.java
/** * Creates a {@link TransformerHandler}. * //from w w w .j a va2 s . com * @param commentHeader the comment header * @param rootTag the root tag * @param streamResult stream result */ public static TransformerHandler createTransformerHandler(String commentHeader, String rootTag, StreamResult streamResult, Charset charset) throws TransformerFactoryConfigurationError, TransformerConfigurationException, SAXException { SAXTransformerFactory tf = (SAXTransformerFactory) TransformerFactory.newInstance(); try { tf.setAttribute("indent-number", new Integer(2)); } catch (Exception e) { // ignore, workaround for JDK 1.5 bug, see http://forum.java.sun.com/thread.jspa?threadID=562510 } TransformerHandler transformerHandler = tf.newTransformerHandler(); Transformer serializer = transformerHandler.getTransformer(); serializer.setOutputProperty(OutputKeys.ENCODING, charset.name()); serializer.setOutputProperty(OutputKeys.METHOD, "xml"); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); transformerHandler.setResult(streamResult); transformerHandler.startDocument(); String newline = System.getProperty("line.separator"); if (newline == null) { newline = "\n"; } commentHeader = (newline + commentHeader).replaceAll("\\n--", newline + " "); transformerHandler.characters("\n".toCharArray(), 0, 1); transformerHandler.comment(commentHeader.toCharArray(), 0, commentHeader.toCharArray().length); transformerHandler.characters("\n".toCharArray(), 0, 1); if (rootTag.length() > 0) { transformerHandler.startElement("", "", rootTag, null); } return transformerHandler; }
From source file:com.sdl.odata.renderer.util.PrettyPrinter.java
/** * Pretty-print a given XML./*from w w w . j a v a 2 s .c o m*/ * * @param xml The not-formatted XML. * @return The pretty-printed XML. */ public static String prettyPrintXml(String xml) throws TransformerException, IOException { Source xmlInput = new StreamSource(new StringReader(xml)); try (StringWriter stringWriter = new StringWriter()) { StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", DEFAULT_INDENT); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name()); transformer.setOutputProperty(OutputKeys.VERSION, "1.0"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } }
From source file:Main.java
/** * Transform document to string representation. * * @param document/*from w ww . j a va 2 s. co m*/ * XHTML document * @return string representation of document * @throws TransformerException * if it is not possible to create a Transformer instance or to transform document */ public static String toString(final Document document) throws TransformerException { final StringWriter stringWriter = new StringWriter(); final StreamResult streamResult = new StreamResult(stringWriter); final Transformer transformer = TRANSFORMER_FACTORY.get().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, TRANSFORMER_YES); // set indent for XML transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2"); // not all JavaSE have the same implementation transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); // transformer.setOutputProperty(OutputKeys.METHOD, "html"); // html method transforms <br/> into <br>, which cannot be re-parsed // transformer.setOutputProperty(OutputKeys.METHOD, "xhtml"); // xhtml method does not work for my xalan transformer transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, TRANSFORMER_YES); transformer.transform(new DOMSource(document.getDocumentElement()), streamResult); return stringWriter.toString(); }
From source file:Main.java
public static String toXMLString(final DOMSource value) { final StringWriter writer = new StringWriter(); try {/*from ww w . j av a 2s .c o m*/ final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.transform(value, new StreamResult(writer)); } catch (final Throwable t) { //todo throw flash error throw new RuntimeException(t); } final String result = writer.toString(); return result.substring(0, result.length() - 1); }