List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
public static void output(Document doc, OutputStream outputStream) throws Exception { TransformerFactory tFactory = TransformerFactory.newInstance(); tFactory.setAttribute("indent-number", 4); Transformer transformer = tFactory.newTransformer(); Properties outputProperties = new Properties(); outputProperties.put(OutputKeys.INDENT, "yes"); outputProperties.put("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.setOutputProperties(outputProperties); DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(new OutputStreamWriter(outputStream, "UTF-8")); transformer.transform(source, result); }
From source file:Main.java
public static String prettyPrint(File file) { Transformer tf;/* www .ja v a 2 s . c o m*/ try { tf = TransformerFactory.newInstance().newTransformer(); tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); tf.setOutputProperty(OutputKeys.INDENT, "yes"); StreamResult stringResult = new StreamResult(new StringWriter()); tf.transform(new DOMSource(convertFileToDom(file)), stringResult); return stringResult.getWriter().toString(); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static void saveXml(Document modDoc, String path) throws TransformerException, IOException { Writer writer = null;/*from w w w .jav a 2s .c o m*/ try { TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(modDoc); writer = getFileWriter(path); Result dest = new StreamResult(writer); aTransformer.setOutputProperty(OutputKeys.INDENT, "yes"); aTransformer.setOutputProperty(OutputKeys.METHOD, "xml"); aTransformer.transform(src, dest); } catch (TransformerException e) { throw e; } finally { writer.close(); } }
From source file:Main.java
public static String XMLToString(Document doc, Boolean singleLine) { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = null; try {//from w w w.ja va 2s .c om transformer = tf.newTransformer(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); StringWriter writer = new StringWriter(); try { transformer.transform(new DOMSource(doc), new StreamResult(writer)); } catch (TransformerException e) { e.printStackTrace(); } String string = writer.getBuffer().toString(); if (!singleLine) { return string; } return string.replaceAll("\n|\r", ""); }
From source file:Main.java
public static void printDocument(Node doc, OutputStream out) { try {// w w w . ja v a2 s .c o m TransformerFactory tf = TransformerFactory.newInstance(); Transformer 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(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); } catch (Exception e) { e.printStackTrace(); } }
From source file:Main.java
/** * Pretty format a given XML document// w w w .j av a 2s . c o m * * @param strInput * Valid XML document (No validity check yet!) * @param nIndent * Indent * @return Formatted XML document * @throws Exception * in error case */ public static String prettyFormat(String strInput, int nIndent) throws Exception { try { Source xmlInput = new StreamSource(new StringReader(strInput)); StringWriter stringWriter = new StringWriter(); StreamResult xmlOutput = new StreamResult(stringWriter); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", nIndent); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(nIndent)); transformer.transform(xmlInput, xmlOutput); return xmlOutput.getWriter().toString(); } catch (Exception e) { // Logger.XMLEval.logState("Pretty formatting: " + e.getMessage(), LogLevel.Error); throw e; } }
From source file:Main.java
public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.setOutputProperty("omit-xml-declaration", "no"); transformer.setOutputProperty("method", "xml"); transformer.setOutputProperty("indent", "yes"); transformer.setOutputProperty("encoding", "UTF-8"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(out, "UTF-8"))); }
From source file:Main.java
/** * Convert XML {@link Document} to its string representation. * * @param document for conversion/*from ww w .ja v a 2 s. c o m*/ * @return - string representation of XML {@link Document} * @throws Exception - if {@link DocumentBuilder} is not initialized */ public static String xmlToString(Document document) throws Exception { if (transformer == null) { throw new Exception("Transformer is null"); } Source xmlSource = new DOMSource(document); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); transformer.transform(xmlSource, result); return stringWriter.toString(); }
From source file:Main.java
public static String documentToString(Document doc, String xslName) { StringWriter sw = new StringWriter(); try {/*from ww w.j a v a 2 s .c om*/ TransformerFactory transfactory = TransformerFactory.newInstance(); Transformer transformer = transfactory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); Source source = new DOMSource(doc.getDocumentElement()); Result result = new StreamResult(sw); transformer.transform(source, result); } catch (Exception ex) { ex.printStackTrace(); } return head + xslName + sw.toString(); }
From source file:Main.java
public static void formatXML(Document xml, Document xsl, URIResolver resolver, Writer output) throws Exception { try {/*from www.j av a2s. c o m*/ DOMSource xslSource = new DOMSource(xsl); DOMSource xmlSource = new DOMSource(xml); Result result = new StreamResult(output); formatXML(xmlSource, xslSource, resolver, result); } finally { output.close(); } }