List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
public static void writeTo(Document document, File output) { try {//from w ww. ja v a 2s . co m TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); FileOutputStream outputstream = new FileOutputStream(output); StreamResult result = new StreamResult(outputstream); // Manually add xml declaration, to force a newline after it. String xmlDeclaration = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"; outputstream.write(xmlDeclaration.getBytes()); // Remove whitespaces outside tags. // Essential to make sure the nodes are properly indented. 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); } // Pretty-print options. transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4"); transformer.transform(source, result); outputstream.close(); } catch (TransformerException | IOException | XPathExpressionException e) { System.out.println("Failed to write document file" + output.getPath() + ": " + e.toString()); } }
From source file:Main.java
public static void sendXml(String result, int operationN) throws Exception { String filepath = "file.xml"; DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docFactory.newDocumentBuilder(); Document doc = docBuilder.parse(filepath); Node tonode = doc.getElementsByTagName("command").item(0); Element res = doc.createElement("result"); res.setTextContent(result);/*from ww w . j a v a 2 s.c o m*/ tonode.appendChild(res); TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(doc); StreamResult result1 = new StreamResult(new File("file2.xml")); StreamResult result3 = new StreamResult(System.out); transformer.transform(source, result1); }
From source file:Main.java
public static String prettyFormat(String xml) { if (xml == null || xml.isEmpty() || !xml.contains("<")) { // System.out.println("Why?"+xml.startsWith("<", 0)); return xml; }//w ww.j ava 2 s. c o m try { Transformer serializer = SAXTransformerFactory.newInstance().newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); //serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes()))); StreamResult res = new StreamResult(new ByteArrayOutputStream()); serializer.transform(xmlSource, res); return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray()).replace("><", ">\n<"); } catch (Exception e) { System.out.println("prettyFormat: Error.." + e.getMessage()); //TODO log error return xml.replace("<", "\n<"); // return xml.replace("><", ">\n<"); } }
From source file:Main.java
public static String format(Document document) throws TransformerException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); final Transformer transformer = TRANSFORMER.get(); transformer.transform(new DOMSource(document), new StreamResult(out)); transformer.reset();//from w ww .jav a 2 s . c om return new String(out.toByteArray(), StandardCharsets.UTF_8); }
From source file:Main.java
public static ByteArrayOutputStream printDOMDocumentToOutputStream(Document doc) { ByteArrayOutputStream os;//from w w w.j a va 2 s . c om os = new ByteArrayOutputStream(); try { // TODO remove, nonsence here XMLSignatureHelper.storeSignatureToXMLFile(doc, "signature.xml"); TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); //trans.setOutputProperty(OutputKeys.ENCODING, "utf-8"); //trans.setOutputProperty(OutputKeys.INDENT, "yes"); //trans.setOutputProperty(OutputKeys.METHOD, "xml"); trans.transform(new DOMSource(doc), new StreamResult(os)); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } return os; }
From source file:Main.java
public static void toText(Node node, OutputStream out) throws TransformerFactoryConfigurationError, TransformerException { Transformer transformer = TransformerFactory.newInstance().newTransformer(); Source source = new DOMSource(node); Result output = new StreamResult(out); transformer.transform(source, output); }
From source file:Main.java
public static String Document2String(Document doc) throws IOException, TransformerException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final TransformerFactory tf = TransformerFactory.newInstance(); final 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(baos, "UTF-8"))); return baos.toString("UTF-8"); }
From source file:Main.java
public static void transfer(Document doc, PrintWriter pw) throws TransformerException { transfer(doc, new StreamResult(pw)); }
From source file:Main.java
/** * Save content of DOM to a file./*w ww . j a va 2 s . c om*/ * @param doc Document * @param filePath String * @throws Exception */ public static void saveDomToFile(Document doc, String filePath) throws Exception { // Write the DOM to file. Source source = new DOMSource(doc); // Prepare the output file File file = new File(filePath); Result result = new StreamResult(file); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.transform(source, result); }
From source file:Main.java
public static void getWordDocument1(ByteArrayOutputStream recvstram, String filename, String xslfilename) throws TransformerConfigurationException, TransformerException, TransformerFactoryConfigurationError { try {/* w w w.j a v a 2s .c o m*/ byte[] myBytes = recvstram.toByteArray(); FileOutputStream out = new FileOutputStream("g:/input.txt"); try { out.write(myBytes); } finally { out.close(); } (TransformerFactory.newInstance().newTransformer(new StreamSource(new File("g:/" + xslfilename)))) .transform(new StreamSource(new File("g:/input.txt")), new StreamResult(new File("g:/channel.doc"))); } catch (Exception e) { } }