List of usage examples for javax.xml.transform Transformer setOutputProperty
public abstract void setOutputProperty(String name, String value) throws IllegalArgumentException;
From source file:com.beligum.core.utils.Toolkit.java
public static String xmlToString(Document document, boolean indent) throws Exception { if (document == null) { return null; } else {//from ww w . j a v a2s . c o m try { StringWriter sw = new StringWriter(); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.transform(new DOMSource(document), new StreamResult(sw)); return sw.toString(); } catch (Exception e) { throw new Exception("Error converting XML to String", e); } } }
From source file:Main.java
/** * @param xml/* w w w . jav a2s . c o m*/ * @return pretty xml */ public static String prettyXml(String xml) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; try { Source source = new StreamSource(new StringReader(xml)); StringWriter writer = new StringWriter(); serializer = tfactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); serializer.transform(source, new StreamResult(writer)); return writer.toString(); } catch (Exception e) { return xml; } }
From source file:Main.java
public static byte[] serializeToByteArray(Document doc) throws IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = null; try {//from w w w . ja va 2s .co m transformer = tFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new IOException("Unable to serialize XML document"); } transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); String encoding = doc.getInputEncoding(); if (encoding == null) encoding = "UTF-8"; transformer.setOutputProperty(OutputKeys.ENCODING, encoding); DOMSource source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); Result result = new StreamResult(out); try { transformer.transform(source, result); } catch (TransformerException e) { throw new IOException("Unable to serialize XML document"); } return out.toByteArray(); }
From source file:Main.java
/** * //w w w .j a v a2s. co m * <B>Purpose:</B> XML transformation using XSL * * @param doc * @param xslInput * @param systemid * @return * @throws TransformerException */ public static Node transform(Document doc, StreamSource xslInput, String systemid) throws TransformerException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(xslInput); DOMResult domResult = new DOMResult(); DOMSource xmlDomSource = null; xmlDomSource = new DOMSource(doc); xmlDomSource.setSystemId(systemid); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", "\n"); transformer.transform(xmlDomSource, domResult); return domResult.getNode(); }
From source file:Main.java
private static String getW3CXmlFromDoc(Document doc) { String xmlString = null;//from ww w . j a v a 2 s . c o m Transformer transformer; try { transformer = TransformerFactory.newInstance().newTransformer(); // transformer.setOutputProperty(OutputKeys.INDENT, "yes"); // yes, // no transformer.setOutputProperty(OutputKeys.INDENT, "no"); // initialize StreamResult with File object to save to file StreamResult xmlStream = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(doc); transformer.transform(source, xmlStream); xmlString = xmlStream.getWriter().toString(); } catch (Exception ex) { logger.error(ex.getMessage()); } return xmlString; }
From source file:com.omertron.thetvdbapi.tools.DOMHelper.java
/** * Convert a DOM document to a string//ww w. ja v a 2s .c o m * * @param doc * @return * @throws TransformerException */ public static String convertDocToString(Document doc) throws TransformerException { //set up a transformer TransformerFactory transfac = TransformerFactory.newInstance(); Transformer trans = transfac.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, YES); trans.setOutputProperty(OutputKeys.INDENT, YES); //create string from xml tree StringWriter sw = new StringWriter(); StreamResult result = new StreamResult(sw); DOMSource source = new DOMSource(doc); trans.transform(source, result); return sw.toString(); }
From source file:com.sun.portal.portletcontainer.admin.PortletRegistryHelper.java
public static synchronized void writeFile(Document document, File file) throws PortletRegistryException { FileOutputStream output = null; try {//w w w. j av a 2 s. c om // Use a Transformer for output TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = tFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); transformer.setOutputProperty(OutputKeys.ENCODING, CharEncoding.UTF_8); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5"); DOMSource source = new DOMSource(document); // StreamResult result = new StreamResult(System.out); output = new FileOutputStream(file); StreamResult result = new StreamResult(output); transformer.transform(source, result); } catch (TransformerConfigurationException tce) { throw new PortletRegistryException(tce); } catch (TransformerException te) { throw new PortletRegistryException(te); } catch (Exception e) { throw new PortletRegistryException(e); } finally { if (output != null) { try { output.close(); } catch (IOException ex) { SilverTrace.warn("portlet", PortletRegistryHelper.class.getSimpleName() + ".writeFile()", "root.EX_NO_MESSAGE", ex); } } } }
From source file:com.omertron.thetvdbapi.tools.DOMHelper.java
/** * Write the Document out to a file using nice formatting * * @param doc The document to save//from w ww. java 2 s .c o m * @param localFile The file to write to * @return */ public static boolean writeDocumentToFile(Document doc, String localFile) { try { TransformerFactory transfact = TransformerFactory.newInstance(); Transformer trans = transfact.newTransformer(); trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, YES); trans.setOutputProperty(OutputKeys.INDENT, YES); trans.transform(new DOMSource(doc), new StreamResult(new File(localFile))); return true; } catch (TransformerConfigurationException ex) { LOG.warn(ERROR_WRITING + localFile, ex); return false; } catch (TransformerException ex) { LOG.warn(ERROR_WRITING + localFile, ex); return false; } }
From source file:Main.java
/** * Serialize XML Document to string using Transformer * /*from www .jav a 2 s. c o m*/ * @param doc XML Document * @return String representation of the the Document * @throws IOException */ public static String serializeToString(Node node, String encoding) throws IOException { TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer transformer = null; try { transformer = tFactory.newTransformer(); } catch (TransformerConfigurationException e) { throw new IOException("Unable to serialize XML document"); } transformer.setOutputProperty(OutputKeys.INDENT, "no"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); DOMSource source = new DOMSource(node); StringWriter writer = new StringWriter(); Result result = new StreamResult(writer); try { transformer.transform(source, result); } catch (TransformerException e) { throw new IOException("Unable to serialize XML document"); } writer.flush(); return writer.toString(); }
From source file:com.ibm.rpe.web.service.docgen.impl.GenerateBaseTemplate.java
private static String transformToString(Document document) { try {/* w ww. j a v a 2 s . co m*/ TransformerFactory transFactory = TransformerFactory.newInstance(); Transformer transformer = transFactory.newTransformer(); StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(new DOMSource(document), new StreamResult(buffer)); return buffer.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }