List of usage examples for javax.xml.transform.stream StreamResult StreamResult
public StreamResult(File f)
From source file:Main.java
/** * To output a DOM as a stream./*from w w w . j a va2s . co m*/ */ public static InputStream documentToPrettyInputStream(Node document) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(document); Result outputTarget = new StreamResult(outputStream); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(xmlSource, outputTarget); InputStream is = new ByteArrayInputStream(outputStream.toByteArray()); return is; }
From source file:Main.java
/** * Save the XML document to a file.//from w ww . j a va 2 s .co m * * @param doc * The XML document to save. * @param file * The file to save the document to. * @param encoding * The encoding to save the file as. * * @throws TransformerException * If there is an error while saving the XML. */ public static void save(Document doc, String file, String encoding) throws TransformerException { try { final Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, encoding); transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); // initialize StreamResult with File object to save to file final Result result = new StreamResult(new File(file)); final DOMSource source = new DOMSource(doc); transformer.transform(source, result); } finally { } }
From source file:Main.java
/** * @param xml//from ww w . j ava 2 s. c om * @return pretty xml */ public static String prettyXml(String xml) { Transformer serializer; try { Source source = new StreamSource(new StringReader(xml)); StringWriter writer = new StringWriter(); serializer = transFactory.newTransformer(); // Setup indenting to "pretty print" serializer.setOutputProperty(OutputKeys.INDENT, "yes"); serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); serializer.transform(source, new StreamResult(writer)); return writer.toString(); } catch (Exception e) { return xml; } }
From source file:Main.java
/** * To output a DOM as a stream./*from w ww . j a v a2 s . c o m*/ */ public static InputStream documentToPrettyInputStream(Document document) throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); Source xmlSource = new DOMSource(document); Result outputTarget = new StreamResult(outputStream); Transformer transformer = TransformerFactory.newInstance().newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.transform(xmlSource, outputTarget); InputStream is = new ByteArrayInputStream(outputStream.toByteArray()); return is; }
From source file:Main.java
public static String documentToString(Document document) throws TransformerException { Node rootNode = (Node) document.getDocumentElement(); Source source = new DOMSource(rootNode); StringWriter stringWriter = new StringWriter(); Result result = new StreamResult(stringWriter); TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer(); transformer.transform(source, result); return stringWriter.getBuffer().toString().replace("\n", ""); }
From source file:Main.java
private static String getIndented(Document aDoc) { String outputXml = ""; try {//ww w . java 2 s . c o m TransformerFactory tf = TransformerFactory.newInstance(); // tf.setAttribute("indent-number", new Integer(4)); Transformer transformer; transformer = tf.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.METHOD, "xml"); transformer.setOutputProperty(OutputKeys.MEDIA_TYPE, "text/xml"); transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); StreamResult result = new StreamResult(new StringWriter()); DOMSource source = new DOMSource(aDoc); transformer.transform(source, result); outputXml = result.getWriter().toString(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } return outputXml; }
From source file:Main.java
public static void writeXml(Document doc, File outputFile) { try {//from w ww .java 2 s .co m Transformer transformer = TransformerFactory.newInstance().newTransformer(); Result output = new StreamResult(outputFile); Source input = new DOMSource(doc); transformer.transform(input, output); } catch (Exception e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:Main.java
License:asdf
public static String documentToString(Document document) { try {/*from w ww.ja v a 2s . c o m*/ TransformerFactory tf = TransformerFactory.newInstance(); Transformer trans = tf.newTransformer(); StringWriter sw = new StringWriter(); trans.transform(new DOMSource(document), new StreamResult(sw)); return sw.toString(); } catch (TransformerException tEx) { tEx.printStackTrace(); } return null; }
From source file:Main.java
public static void marshalToStream(Element elm, OutputStream ostream, boolean indent) throws Exception { TransformerFactory transFac = TransformerFactory.newInstance(); Transformer trans = transFac.newTransformer(); if (indent) { trans.setOutputProperty(OutputKeys.INDENT, "yes"); trans.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); }//from www .j ava 2s . c o m trans.setOutputProperty(OutputKeys.STANDALONE, "no"); trans.transform(new DOMSource(elm), new StreamResult(ostream)); }
From source file:Main.java
public static void createXML222(String xmlFile, String xpath, String element, String data) { try {/*from w w w. j av a 2 s .c o m*/ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); Document document = documentBuilder.newDocument(); Element rootElement = document.createElement(xpath); document.appendChild(rootElement); for (int i = 1; i <= 1; i++) { Element em = document.createElement(element); em.appendChild(document.createTextNode(data)); rootElement.appendChild(em); } TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); DOMSource source = new DOMSource(document); FileOutputStream fo = new FileOutputStream(xmlFile); StreamResult result = new StreamResult(fo); transformer.transform(source, result); } catch (Exception ex) { ex.printStackTrace(); } }