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 byte[] doc2bytes(Document doc, boolean formated) { try {/*from ww w. j av a 2 s . com*/ Source source = new DOMSource(doc); ByteArrayOutputStream out = new ByteArrayOutputStream(); Result result = new StreamResult(out); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); if (formated) { // linefeed formatting transformer.setOutputProperty(OutputKeys.INDENT, "yes"); } else { // remove xml header transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } transformer.transform(source, result); return out.toByteArray(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String prettyXML(String xml, int indent) { try {/*from ww w . java 2s .c om*/ // Turn xml string into a document Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder() .parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8")))); // Remove whitespaces outside tags XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nodeList = (NodeList) xPath.evaluate("//text()[normalize-space()='']", document, XPathConstants.NODESET); for (int i = 0; i < nodeList.getLength(); ++i) { Node node = nodeList.item(i); node.getParentNode().removeChild(node); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", indent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); StringWriter stringWriter = new StringWriter(); transformer.transform(new DOMSource(document), new StreamResult(stringWriter)); return stringWriter.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:Main.java
private static Transformer getTransformer() throws TransformerConfigurationException { TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); // Formatting XML properly transformer.setOutputProperty(OutputKeys.INDENT, "yes"); return transformer; }
From source file:Main.java
/** * @param xsltFile The XSLT stylesheet file * @param parms XSL parameters to pass to the stylesheet. * @return the configured XSLT transformer * @throws TransformerException if there is a problem configuring the transformer *//*from w ww . j av a 2 s .c o m*/ static Transformer createTransformer(final File xsltFile, final Map<String, String> parms) throws TransformerException { try { Source xslSource = new StreamSource(xsltFile); TransformerFactory transFact = TransformerFactory.newInstance(); transFact.setAttribute("indent-number", 2); Transformer transformer = transFact.newTransformer(xslSource); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); for (String parm : parms.keySet()) { transformer.setParameter(parm, parms.get(parm)); } return transformer; } catch (TransformerConfigurationException e) { e.printStackTrace(); throw e; } }
From source file:Main.java
public static String transform2String(Node node) throws TransformerException { final TransformerFactory transFactory = TransformerFactory.newInstance(); final Transformer transformer = transFactory.newTransformer(); final StringWriter buffer = new StringWriter(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.transform(new DOMSource(node), new StreamResult(buffer)); return buffer.toString(); }
From source file:Main.java
/** * Writes an xml representation to a stream. * * @param doc/*from ww w. ja v a2 s .com*/ * the document * @param out * the output stream * @throws IOException * if there is an error transforming the dom to a stream */ public static void toXml(Document doc, OutputStream out) throws IOException { try { DOMSource domSource = new DOMSource(doc); StreamResult result = new StreamResult(out); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.VERSION, doc.getXmlVersion()); transformer.transform(domSource, result); } catch (TransformerException e) { throw new IOException("unable to transform dom to a stream"); } }
From source file:Main.java
public static void saveXml(Document doc, String filename) { try {/*from w w w . ja v a 2 s .c o m*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(doc); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); PrintWriter pw = new PrintWriter(new FileOutputStream(filename)); StreamResult result = new StreamResult(pw); transformer.transform(source, result); } catch (TransformerException tfe) { tfe.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:Main.java
private static String convertDOMToString(DOMSource source, boolean outputXmlDeclaration) { StreamResult result = null;/*ww w .j ava2s .co m*/ try { Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); if (!outputXmlDeclaration) { transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); } result = new StreamResult(new StringWriter()); transformer.transform(source, result); } catch (TransformerException e) { } return result.getWriter().toString(); }
From source file:Main.java
/** * Convert the document to an array of bytes. * * @param doc The XML document./*from w ww . j a v a 2 s . c om*/ * @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); try { return writer.getBuffer().toString().getBytes(encoding); } catch (UnsupportedEncodingException e) { throw new TransformerException("Unsupported Encoding", e); } }
From source file:Main.java
/** * Transforms an XML to a String/*from w ww . java2 s . c o m*/ * @param node XML node * @return String represenation of XML */ public static String printXML(Node node) { TransformerFactory tfactory = TransformerFactory.newInstance(); Transformer serializer; try { serializer = tfactory.newTransformer(); serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); StringWriter output = new StringWriter(); serializer.transform(new DOMSource(node), new StreamResult(output)); return output.toString(); } catch (TransformerException e) { throw new RuntimeException(e); } }