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
/** * output XML data in a file/*w ww . j a v a2 s . co m*/ * * @param doc * @param path */ public static void buildXmlFile(Document doc, String path) { TransformerFactory tfactory = TransformerFactory.newInstance(); try { Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("encoding", "UTF-8"); transformer.transform(source, result); } catch (TransformerConfigurationException e) { //TODO } catch (TransformerException e) { } }
From source file:com.c4om.utils.xmlutils.JavaXMLUtils.java
/** * Method that converts a W3C {@link Document} object to a String * @param document the document to convert * @param indent whether lines must be indented or not * @param omitDeclaration whether the XML declaration should be omitted or not. * @param encoding the encoding placed at the XML declaration. Be carefully: Specifying here an encoding only takes effect at the XML declaration. If you are going to write the string to an output stream (e.g. to a file), you must also take care of the encoding there, for example, by means of {@link org.apache.commons.io.output.XmlStreamWriter}. * @return the {@link String} representation of the document * @throws TransformerException if there are problems during the conversion *//* w w w . ja v a 2 s . c o m*/ public static String convertW3CDocumentToString(Document document, boolean indent, boolean omitDeclaration, String encoding) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); } StringWriter writer = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(writer)); String output = writer.getBuffer().toString(); return output; }
From source file:hu.bme.mit.sette.common.util.XmlUtils.java
/** * Transforms the specified XML document to the specified result. * * @param document/*from w ww.ja v a2 s .c o m*/ * The XML document. * @param result * The result. * @throws TransformerException * If an unrecoverable error occurs during the course of the * transformation. */ private static void transformXml(final Document document, final Result result) throws TransformerException { Validate.notNull(document, "The document must not be null"); Validate.notNull(result, "The result must not be null"); // write XML to stream Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(document), result); }
From source file:Main.java
public static void buildXmlFile(Document doc, String path) { TransformerFactory tfactory = TransformerFactory.newInstance(); try {/*from w ww . j a v a 2 s .c om*/ Transformer transformer = tfactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new File(path)); transformer.setOutputProperty("encoding", "gb2312"); transformer.transform(source, result); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.c4om.utils.xmlutils.JavaXMLUtils.java
/** * Method that prints a W3C {@link Document} object to a provided {@link OutputStream}. * Encoding should be correctly specified. * @param document the document to convert * @param os the output stream to print to * @param indent whether lines must be indented or not * @param omitDeclaration whether the XML declaration should be omitted or not. * @param encoding the encoding placed at the XML declaration and used to encode the file. * @return the {@link String} representation of the document * @throws TransformerException if there are problems during the conversion *//*from w ww.j a v a 2 s . co m*/ public static void printW3CDocumentToOutputStream(Document document, OutputStream os, boolean indent, boolean omitDeclaration, String encoding) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omitDeclaration ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); XmlStreamWriter writer; if (encoding != null) { transformer.setOutputProperty(OutputKeys.ENCODING, encoding); writer = new XmlStreamWriter(os, encoding); } else { writer = new XmlStreamWriter(os); } StreamResult streamResult = new StreamResult(writer); transformer.transform(new DOMSource(document), streamResult); }
From source file:Main.java
public static void printNode(OutputStream out, Node node, boolean prettyPrint, boolean includeXmlDeclaration) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; try {//from w ww . j a v a 2 s.c om 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:com.ackpdfbox.app.imageio.MetaUtil.java
static void debugLogMetadata(IIOMetadata metadata, String format) { if (!LOG.isDebugEnabled()) { return;/* www .j a v a2 s .c o m*/ } // see http://docs.oracle.com/javase/7/docs/api/javax/imageio/ // metadata/doc-files/standard_metadata.html IIOMetadataNode root = (IIOMetadataNode) metadata.getAsTree(format); try { StringWriter xmlStringWriter = new StringWriter(); StreamResult streamResult = new StreamResult(xmlStringWriter); Transformer transformer = TransformerFactory.newInstance().newTransformer(); // see http://stackoverflow.com/a/1264872/535646 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); DOMSource domSource = new DOMSource(root); transformer.transform(domSource, streamResult); LOG.debug("\n" + xmlStringWriter); } catch (IllegalArgumentException ex) { LOG.error(ex, ex); } catch (TransformerException ex) { LOG.error(ex, ex); } }
From source file:eu.eidas.engine.test.simple.SSETestUtils.java
/** * Prints the tree DOM.//from w ww . j a va2s . c o m * * @param samlToken the SAML token * @param isIndent the is indent * * @return the string * @throws TransformerException the exception */ public static String printTreeDOM(final Element samlToken, final boolean isIndent) throws TransformerException { // set up a transformer final TransformerFactory transfac = TransformerFactory.newInstance(); final Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); trans.setOutputProperty(OutputKeys.INDENT, String.valueOf(isIndent)); // create string from XML tree final StringWriter stringWriter = new StringWriter(); final StreamResult result = new StreamResult(stringWriter); final DOMSource source = new DOMSource(samlToken); trans.transform(source, result); final String xmlString = stringWriter.toString(); return xmlString; }
From source file:net.sf.jabref.exporter.OpenOfficeDocumentCreator.java
private static void exportOpenOfficeCalcXML(File tmpFile, BibDatabase database, List<BibEntry> entries) { OOCalcDatabase od = new OOCalcDatabase(database, entries); try (Writer ps = new OutputStreamWriter(new FileOutputStream(tmpFile), StandardCharsets.UTF_8)) { DOMSource source = new DOMSource(od.getDOMrepresentation()); StreamResult result = new StreamResult(ps); Transformer trans = TransformerFactory.newInstance().newTransformer(); trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.transform(source, result); } catch (Exception e) { throw new Error(e); }/* w w w . j a v a 2 s .c o m*/ }
From source file:Signing.java
private static void dumpDocument(Node root) throws TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(root), new StreamResult(System.out)); }/*from w w w .j a v a 2s .c o m*/