List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
public static boolean ExportXML(Document xmlOutput, String filename) { try {//from www. j a v a 2 s .c o m if (xmlOutput != null) { Source source = new DOMSource(xmlOutput); FileOutputStream file = new FileOutputStream(filename); StreamResult res = new StreamResult(file); Transformer xformer = TransformerFactory.newInstance().newTransformer(); xformer.setOutputProperty(OutputKeys.ENCODING, "ISO8859-1"); xformer.setOutputProperty(OutputKeys.INDENT, "yes"); xformer.transform(source, res); file.close(); return true; } return false; } catch (FileNotFoundException e) { return false; } catch (TransformerConfigurationException e) { return false; } catch (TransformerException e) { return false; } catch (IOException e) { return false; } }
From source file:Main.java
public static String nodeToString(Node n) { init();//from w w w . j av a2s .com try { DOMSource src = new DOMSource(n); StringWriter sr = new StringWriter(); Result res = new StreamResult(sr); tx.transform(src, res); return sr.toString(); } catch (Exception e) { return (e.getMessage()); } }
From source file:Main.java
/** * Creates a {@link String} out of a {@link Node} * //www .ja va 2 s .com * @param node * never <code>null</code> * @return the node as string, never <code>null</code> * @throws Exception */ public static String asString(Node node) throws Exception { StringWriter writer = new StringWriter(); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.transform(new DOMSource(node), new StreamResult(writer)); return writer.toString(); }
From source file:Main.java
public static String documentToString(Document doc) throws TransformerConfigurationException, TransformerException, IOException { //return doc.getDocumentElement().toString(); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); DOMSource src = new DOMSource(doc); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try {/*from ww w .j a v a 2 s . c o m*/ StreamResult sr = new StreamResult(baos); transformer.transform(src, sr); String result = baos.toString(); return result; } finally { baos.close(); } }
From source file:Main.java
public static InputStream newInputStreamFromDocument(Document doc) throws TransformerConfigurationException, TransformerException { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(doc); Result outputTarget = new StreamResult(outputStream); TransformerFactory.newInstance().newTransformer().transform(xmlSource, outputTarget); return new ByteArrayInputStream(outputStream.toByteArray()); }
From source file:Main.java
/** Write an XML DOM tree to a file * * @param doc the document to write/*from w w w . j a va 2s. c om*/ * @param file the file to write to * @throws IOException if a file I/O error occurs */ public static void write(Document doc, File file) throws IOException { try { DOMSource domSource = new DOMSource(doc); FileOutputStream stream = new FileOutputStream(file); // OutputStreamWriter works around indent bug 6296446 in JDK // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446 StreamResult streamResult = new StreamResult(new OutputStreamWriter(stream)); TransformerFactory tf = TransformerFactory.newInstance(); tf.setAttribute("indent-number", new Integer(2)); Transformer serializer = tf.newTransformer(); serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.transform(domSource, streamResult); } catch (TransformerException e) { // This exception is never thrown, treat as fatal if it is throw new RuntimeException(e); } }
From source file:Main.java
public static String prettyPrintXML(String source) { try {//from ww w. jav a2s . c o m ByteArrayOutputStream baos = new ByteArrayOutputStream(); TransformerFactory tff = TransformerFactory.newInstance(); Transformer tf; tf = tff.newTransformer(); tf.setOutputProperty(OutputKeys.INDENT, "yes"); tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(baos); tf.transform(new StreamSource(new StringReader(source)), result); return new String(baos.toByteArray()); } catch (Exception e) { return source; } }
From source file:Main.java
/** * Represent xml node as string//from w w w.j ava 2 s. c o m * @param node to be represented * @return node text */ public static String xmlToString(Node node) { try { Source source = new DOMSource(node); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); Transformer transformer = getTransformerFactory().newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString(); } catch (TransformerException e) { throw new RuntimeException("Can't transfor XML to String", e); } }
From source file:Main.java
public static void document2File(Document document, String encode, File file) throws TransformerException { TransformerFactory tf = TransformerFactory.newInstance(); Transformer transformer = tf.newTransformer(); DOMSource source = new DOMSource(document); transformer.setOutputProperty("encoding", encode); transformer.setOutputProperty("indent", "yes"); transformer.transform(source, new StreamResult(file)); }
From source file:Main.java
/** * Writes an XML document to an output stream. * * @param document//from ww w . j a v a 2s . co m * @param outputStream * @throws TransformerException if the output failed */ public static void writeDocument(Document document, OutputStream outputStream, String encoding) throws TransformerException { Source source = new DOMSource(document); Result result = new StreamResult(outputStream); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty("indent", "yes"); // make the output easier to read, see Transformer.getOutputProperties transformer.setOutputProperty("encoding", encoding); transformer.transform(source, result); }