List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
public static void flush(Document in, OutputStream out) throws RuntimeException { try {/* ww w. j a v a 2 s. c o m*/ // Write the content into XML file Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); transformer.setOutputProperty(OutputKeys.STANDALONE, "no"); transformer.transform(new DOMSource(in), new StreamResult(out)); } catch (TransformerException e) { throw new RuntimeException("Exception flush document", e); } }
From source file:Main.java
public static void writeXml(Document doc, OutputStream outputStream) { try {// ww w. j a v a 2 s . c o m // Prepare the DOM document for writing Source source = new DOMSource(doc); // Prepare the output file Result result = new StreamResult(outputStream); // Write the DOM document to the file Transformer xformer = TransformerFactory.newInstance().newTransformer(); try { xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "3"); } catch (IllegalArgumentException e) { // ignore } try { xformer.setOutputProperty(OutputKeys.INDENT, "yes"); } catch (IllegalArgumentException e) { // ignore } xformer.transform(source, result); } catch (TransformerConfigurationException e) { throw new IllegalStateException(e); } catch (TransformerException e) { throw new IllegalStateException(e); } }
From source file:Main.java
/** * Writes the content of the given node into the given stream *//* w w w . j a v a 2s . co m*/ public static void writeNode(final Node node, final OutputStream out) { write(node, new StreamResult(out)); }
From source file:Main.java
public static void xmlToFile(Document doc, String fileNameToWrite) throws Exception { DOMSource domSource = new DOMSource(doc); PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(fileNameToWrite))); StreamResult streamResult = new StreamResult(out); TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); transformer.transform(domSource, streamResult); }
From source file:Main.java
public static boolean saveDocument(String fileName, Document doc) { System.out.println("Saving XML file... " + fileName); // open output stream where XML Document will be saved File xmlOutputFile = new File(fileName); FileOutputStream fos;//from w w w.j a va2s . c o m Transformer transformer; try { fos = new FileOutputStream(xmlOutputFile); } catch (FileNotFoundException e) { System.out.println("Error occured: " + e.getMessage()); return false; } // Use a Transformer for output TransformerFactory transformerFactory = TransformerFactory.newInstance(); try { transformer = transformerFactory.newTransformer(); } catch (TransformerConfigurationException e) { System.out.println("Transformer configuration error: " + e.getMessage()); return false; } DOMSource source = new DOMSource(doc); StreamResult result = new StreamResult(fos); // transform source into result will do save try { transformer.transform(source, result); } catch (TransformerException e) { System.out.println("Error transform: " + e.getMessage()); } return true; }
From source file:Main.java
private static String getNodeValue(NodeList nodeList) throws TransformerFactoryConfigurationError, TransformerException { final Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.setURIResolver(null);/*from ww w .j a va2s .c o m*/ final StringBuilder buf = new StringBuilder(); for (int i = 0; i < nodeList.getLength(); i++) { final StringWriter sw = new StringWriter(); serializer.transform(new DOMSource(nodeList.item(i)), new StreamResult(sw)); String xml = sw.toString(); final Matcher matcher = HEADER_PATTERN.matcher(xml); if (matcher.matches()) { xml = matcher.group(1); } buf.append(xml); buf.append("\n"); } return buf.toString(); }